PHP
  Home arrow PHP arrow Page 3 - 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 - 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
     

       

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek