PHP
  Home arrow PHP arrow Page 3 - Understanding Static Properties with P...
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 
Moblin 
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? 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Understanding Static Properties with PHP 5 - Defining static properties within PHP 5 classes


    (Page 3 of 4 )

    In consonance with the concepts that I explained during the first article of this series, you probably remember the definition of a static property in PHP 5. Just in case you don't, let me refresh your memory. In crude terms, when a class property is declared static, this means that that it will be unique for the class, and at the same time, its value will be shared by all its eventual instances.

    While the previous definition may sound a bit complex at first sight, the truth is that implementing a static property in a useful fashion is a straightforward process that can be performed with minor problems. However, to clarify this rather abstract concept, in the next few lines I'll build a couple of primitive PHP classes for displaying basic information about some fictional users.

    As you'll see in a moment, these classes will declare a static property inside their API, which will allow different instances of the classes in question to link with each other. But it's time to finish with the boring theory and see how the classes look, so here are their corresponding signatures:

    // define abstract 'User' class - declares '$nextUserID' property
    static (it can be accessed by all the classes)
    abstract class User{
      static public $nextUserID=1;
      public $userID;
      abstract public function __construct($fname,$lname,$email);
      abstract public function getFirstName();
      abstract public function getLastName();
      abstract public function getEmail();
      abstract public function getID();
    }

    // define 'ForumUser' class
    class ForumUser extends User{
      public function __construct($fname='John',$lname='Doe',$email='john@domain.com'){
        if(!$fname){
          throw new Exception('Invalid First Name for forum user.');
        }
        if(!$lname){
          throw new Exception('Invalid Last Name for forum user.');
        }
        if(!$email||preg_match("/^.+@.+..+$/",$email)){
          throw new Exception('Invalid email address for forum
    user.');
        }
        $this->fname=$fname;
        $this->lname=$lname;
        $this->email=$email;
        $this->userID=self::$nextUserID++;
      }

      // get user's First Name
      public function getFirstName(){
        return $this->fname;
      }

      // get user's Last Name
      public function getLastName(){
        return $this->lname;
      }

      // get user's email address
      public function getEmail(){
        return $this->email;
      }

      // get user's ID
      public function getID(){
        return $this->userID;
      }
    }

    As you can see, I defined two basic PHP classes. The first one is very convenient for modeling the generic structure of a fictional user, and the second one, "ForumUser," implements all the abstract methods defined by its corresponding parent. So far, this process is quite easy to follow and shouldn't be difficult to understand at all.

    However, I'd like you to focus your attention on the definition of the "$nextUserID" property. In this case, this property has been declared static and also initialized with a value of 1, which means that it can be shared by all the instances created from the base class. Indeed, declaring this property implies that it can be incremented, decremented or whatever you can think of, by the distinct instances of the originating class. This is clearly demonstrated by the definition of the constructor belonging to the "ForumUser" class:

    public function __construct($fname='John',$lname='Doe',
    $email='john@domain.com'){
      if(!$fname){
        throw new Exception('Invalid First Name for forum user.');
      }
      if(!$lname){
        throw new Exception('Invalid Last Name for forum user.');
      }
      if(!$email||preg_match("/^.+@.+..+$/",$email)){
        throw new Exception('Invalid email address for forum user.');
      }
      $this->fname=$fname;
      $this->lname=$lname;
      $this->email=$email;
      $this->userID=self::$nextUserID++;
    }

    As shown above, each time a new forum user object is created, it takes the value of the static $nextUserID property and increments it, in this way producing a chaining effect across all the user objects. Indeed, the previous sample class very clearly demonstrates how a static property can be shared by several objects in a helpful manner, and it extends the excellent implementation of static data in PHP, provided by Paul Hudson on his web site located at: http://www.hudzilla.org

    All right, having said this, I think it's time to move on and demonstrate how the previous "ForumUser" class can be used in a concrete situation to illustrate the actual functionality of its "$nextUserID" property. So go ahead and visit the following section. I'll be there, waiting for you.

    More PHP Articles
    More By Alejandro Gervasio


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

       

    PHP ARTICLES

    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - 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...




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