HomePHP Page 3 - Understanding Static Properties with PHP 5
Defining static properties within PHP 5 classes - PHP
The powerful object model offered by PHP 5 presents some useful features that are quite frequently underestimated by the average PHP developer. This is precisely the case with using static methods and properties in conjunction with the object-oriented paradigm, thus if you're interested in learning how to put static data to work for you, then this series of articles might be what you're looking for.
In consonance with the concepts that I explained during the first article of this series, you probably remember the definition of a static property in PHP 5. Just in case you don't, let me refresh your memory. In crude terms, when a class property is declared static, this means that that it will be unique for the class, and at the same time, its value will be shared by all its eventual instances.
While the previous definition may sound a bit complex at first sight, the truth is that implementing a static property in a useful fashion is a straightforward process that can be performed with minor problems. However, to clarify this rather abstract concept, in the next few lines I'll build a couple of primitive PHP classes for displaying basic information about some fictional users.
As you'll see in a moment, these classes will declare a static property inside their API, which will allow different instances of the classes in question to link with each other. But it's time to finish with the boring theory and see how the classes look, so here are their corresponding signatures:
// 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; } }
As you can see, I defined two basic PHP classes. The first one is very convenient for modeling the generic structure of a fictional user, and the second one, "ForumUser," implements all the abstract methods defined by its corresponding parent. So far, this process is quite easy to follow and shouldn't be difficult to understand at all.
However, I'd like you to focus your attention on the definition of the "$nextUserID" property. In this case, this property has been declared static and also initialized with a value of 1, which means that it can be shared by all the instances created from the base class. Indeed, declaring this property implies that it can be incremented, decremented or whatever you can think of, by the distinct instances of the originating class. This is clearly demonstrated by the definition of the constructor belonging to the "ForumUser" class:
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++; }
As shown above, each time a new forum user object is created, it takes the value of the static $nextUserID property and increments it, in this way producing a chaining effect across all the user objects. Indeed, the previous sample class very clearly demonstrates how a static property can be shared by several objects in a helpful manner, and it extends the excellent implementation of static data in PHP, provided by Paul Hudson on his web site located at: http://www.hudzilla.org
All right, having said this, I think it's time to move on and demonstrate how the previous "ForumUser" class can be used in a concrete situation to illustrate the actual functionality of its "$nextUserID" property. So go ahead and visit the following section. I'll be there, waiting for you.