Understanding Static Properties with PHP 5 - Demonstrating the functionality of a static property (
Page 4 of 4 )
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{
// 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 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!