SunQuest
 
       PHP
  Home arrow PHP arrow Page 4 - Understanding Static Properties with P...
Click Here
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
SunQuest
 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? 
PHP

Understanding Static Properties with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Over the course of this second installment of the series, you'll learn the core...
     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway