As I stated in the previous section, probably the best way to illustrate how the static property belonging to the previous "ForumUser" class can be used is by setting up an example. In this example, some fictional user objects are created, and in consequence, the "$nextUserID" is incremented in each case. That being explained, here is the corresponding code sample: try{ // display information on first forum user /* Data for first forum user is as following: First Name: Robert */ // display information on second forum user /* Data for second forum user is as following: First Name: John */
// display information on third forum user /* Data for third forum user is as following: First Name: Susan */ // display information on last forum user /* Data for last forum user is as following: First Name: Mary */ As demonstrated above, after creating some hypothetical forum user objects, their personal data is displayed on the browser via the corresponding accessing methods. Nonetheless, the most interesting thing to note here is how the static $nextUserID" property is incremented each time a new user object is instantiated, in this way showing in a nutshell how the property in question is shared by all the instances of the previous "ForumUser" class. Finally, as usual with many of my articles on PHP development, feel free to modify the source code of all the sample classes built here. This will help you improve your existing background in using static properties with PHP 5. Happy coding! Final thoughts In this second part of the series, I showed you how to define and utilize a couple of PHP classes which incorporate a static property within their API. Nevertheless, this instructive journey isn't finished yet, since in the last tutorial I'll teach you how to apply the observer pattern in conjunction with a unique static property to build a data validation system. The proposal is actually interesting, so I hope to see you there!
blog comments powered by Disqus |
|
|
|
|
|
|
|