Home arrow PHP arrow Page 4 - Understanding Static Properties with PHP 5

Demonstrating the functionality of a static property - 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.

TABLE OF CONTENTS:
  1. Understanding Static Properties with PHP 5
  2. Reintroducing a previous hands-on example
  3. Defining static properties within PHP 5 classes
  4. Demonstrating the functionality of a static property
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
September 12, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: