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 // define 'ForumUser' class // get user's First Name // get user's Last Name // get user's email address // get user's ID 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', 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.
blog comments powered by Disqus |