HomePHP Page 2 - Using the Observer Design Pattern with Static Data in PHP 5
Handling user data via a single static property - PHP
In the vast terrain of object-oriented programming with PHP 5, working with static data members is an approach that provides developers with the capacity for building classes that are callable from outside the object context. It also lets them define properties that are shared by all the instances of those classes. If you're interested in learning how to put this powerful feature to work for you, then you should start reading this series of articles now!
Before I continue demonstrating how useful a single static property can be in the context of object-oriented programming with PHP 5, I want to reintroduce the hands-on example developed in the preceding article of this series. This will make it fresh in your mind, which should really help if you want to reproduce the business logic implemented by it within your own PHP applications.
As you probably recall, the example in question illustrated how a group of instances belonging to the same originating class can be easily linked with each other by using a single static property. The signatures corresponding each of the sample classes used for that specific occasion looked like this:
// define abstract 'User' class - declares '$nextUserID' property static (it can be accessed by all the classes) abstract class User{ static public $nextUserID=1; public $userID; abstract public function __construct($fname,$lname,$email); abstract public function getFirstName(); abstract public function getLastName(); abstract public function getEmail(); abstract public function getID(); }
// define 'ForumUser' class class ForumUser extends User{ public function __construct ($fname='John',$lname='Doe',$email='john@domain.com'){ if(!$fname){ throw new Exception('Invalid First Name for forum user.'); } if(!$lname){ throw new Exception('Invalid Last Name for forum user.'); } if(!$email||preg_match("/^.+@.+..+$/",$email)){ throw new Exception('Invalid email address for forum user.'); } $this->fname=$fname; $this->lname=$lname; $this->email=$email; $this->userID=self::$nextUserID++; }
// get user's First Name public function getFirstName(){ return $this->fname; }
// get user's Last Name public function getLastName(){ return $this->lname; }
// get user's email address public function getEmail(){ return $this->email; }
// get user's ID public function getID(){ return $this->userID; } } try{ // create some forum user objects $user1=new ForumUser('Robert','Wilson','bob@domain.com'); $user2=new ForumUser('John','Smith','johnny@domain.com'); $user3=new ForumUser('Susan','Jackson','suse@domain.com'); $user4=new ForumUser('Mary','King','mary@domain.com');
// display information on first forum user echo '<h2>Data for first forum user is as following:</h2>'; echo '<p>First Name: '.$user1->getFirstName().'</p><p>Last Name: '.$user1->getLastName().'<p/><p>Email address: '.$user1- >getEmail().'</p><p>User ID: '.$user1->getID().'</p>'; /* displays the following:
Data for first forum user is as following: First Name: Robert Last Name: Wilson Email address: bob@domain.com User ID: 1 */
// display information on second forum user echo '<h2>Data for second forum user is as following:</h2>'; echo '<p>First Name: '.$user2->getFirstName().'</p><p>Last Name: '.$user2->getLastName().'<p/><p>Email address: '.$user2- >getEmail().'</p><p>User ID: '.$user2->getID().'</p>'; /* displays the following:
Data for second forum user is as following: First Name: John Last Name: Smith Email address: johnny@domain.com User ID: 2 */
// display information on third forum user echo '<h2>Data for third forum user is as following:</h2>'; echo '<p>First Name: '.$user3->getFirstName().'</p><p>Last Name: '.$user3->getLastName().'<p/><p>Email address: '.$user3- >getEmail().'</p><p>User ID: '.$user3->getID().'</p>'; /* displays the following:
Data for third forum user is as following: First Name: Susan Last Name: Jackson Email address: suse@domain.com User ID: 3 */
// display information on last forum user echo '<h2>Data for last forum user is as following:</h2>'; echo '<p>First Name: '.$user4->getFirstName().'</p><p>Last Name: '.$user4->getLastName().'<p/><p>Email address: '.$user4- >getEmail().'</p><p>User ID: '.$user4->getID().'</p>'; /* displays the following:
Data for last forum user is as following: First Name: Mary Last Name: King Email address: mary@domain.com User ID: 4 */ } catch(Exception $e){ echo $e->getMessage(); exit(); }
As you can see, the previous sample classes implement a primitive application for displaying some basic data about a few fictional users, such as their first and last names, along with their email addresses. However, the most interesting thing to note here is the utilization of a single static "$nextUserID" property, which is used by all the instances of the originating "ForumUser" class to increment the ID of the next user. Indeed, this implementation of a given static property shows in a nutshell its remarkable functionality, particularly when it comes to enchaining several instances of a specific class.
So far, so good. At this stage, you hopefully grasped the logic that drives the above hands-on example. So let's take a look at another case where the use of a single static property can be really handy.
In this case, I'll show you how to build an expandable data validation system utilizing a basic static property in conjunction with the programmatic model dictated by the popular Observer design pattern.
Want to see how this will be achieved? Jump ahead and read the next few lines.