PHP
  Home arrow PHP arrow Page 4 - Understanding Static Properties with PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Understanding Static Properties with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2007-09-12


    Table of Contents:
  • Understanding Static Properties with PHP 5
  • Reintroducing a previous hands-on example
  • Defining static properties within PHP 5 classes
  • Demonstrating the functionality of a static property

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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!



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek