PHP
  Home arrow PHP arrow Page 2 - 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 - Reintroducing a previous hands-on example
    ( Page 2 of 4 )

    For the sake of completeness, before I start explaining how to define and use static properties in a helpful fashion with PHP 5, first I'd like to remind you quickly of the implementation of a static method as part of the so-called factory pattern that was discussed in the preceding tutorial of the series. In doing so, hopefully you'll be better prepared to take the next step in this learning process and grasp more quickly the usage of static properties in PHP 5 classes.

    So, having explained that, now have a look at the following sample PHP classes. These classes use a static method to create some basic DIV objects. Here they are: 

    // define abstract 'DivElement' class
    abstract class DivElement{
      private $id;
      private $class;
      private $content;
      abstract public function __construct($content='This is the
    content for the DIV element.',$id='divid',$class='divclass');
      abstract public function display();          
    }

    // define concrete 'AbsoluteDivElement' class
    class AbsoluteDivElement extends DivElement{
      public function __construct($content='This is the content for
    the DIV element.',$id='divid',$class='divclass'){
        if(!$content){
          throw new Exception('Invalid content for the DIV element');
        }
        if(strlen($id)>16||!preg_match("/[a-z]/",$id)){
          throw new Exception('Invalid ID attribute for the DIV
    element.');
        }
        if(strlen($class)>16||!preg_match("/[a-z]/",$class)){
          throw new Exception('Invalid class attribute for the DIV
    element.');
        }
        $this->content=$content;
        $this->id=$id;
        $this->class=$class;     
      }
      public function display(){
        $html='<div';
        if($this->id){
          $html.=' id="'.$this->id.'"';
        }
        if($this->class){
          $html.=' class="'.$this->class.'"';
        }
        $html.=' style="position: absolute;top: 100px;left:
    10px;">'.$this->content.'</div>';
        return $html;
      }          
    }

    // define concrete 'LeftFloatedDivElement' class
    class LeftFloatedDivElement extends DivElement{
      public function __construct($content='This is the content for
    the DIV element.',$id='divid',$class='divclass'){
        if(!$content){
          throw new Exception('Invalid content for the DIV element');
        }
        if(strlen($id)>16||!preg_match("/[a-z]/",$id)){
          throw new Exception('Invalid ID attribute for the DIV
    element.');
        }
        if(strlen($class)>16||!preg_match("/[a-z]/",$class)){
          throw new Exception('Invalid class attribute for the DIV
    element.');
        }
        $this->content=$content;
        $this->id=$id;
        $this->class=$class;     
      }

      public function display(){
        $html='<div';
        if($this->id){
          $html.=' id="'.$this->id.'"';
        }
        if($this->class){
          $html.=' class="'.$this->class.'"';
        }
        $html.=' style="float: left;">'.$this->content.'</div>';
        return $html;
      }          
    }

    // define concrete 'RightFloatedDivElement' class
    class RightFloatedDivElement extends DivElement{
      public function __construct($content='This is the content for
    the DIV element.',$id='divid',$class='divclass'){
        if(!$content){
          throw new Exception('Invalid content for the DIV element');
        }
        if(strlen($id)>16||!preg_match("/[a-z]/",$id)){
          throw new Exception('Invalid ID attribute for the DIV
    element.');
        }
        if(strlen($class)>16||!preg_match("/[a-z]/",$class)){
          throw new Exception('Invalid class attribute for the DIV
    element.');
        }
        $this->content=$content;
        $this->id=$id;
        $this->class=$class;     
      }

      public function display(){
        $html='<div';
        if($this->id){
          $html.=' id="'.$this->id.'"';
        }
        if($this->class){
          $html.=' class="'.$this->class.'"';
        }
        $html.=' style="float: right;">'.$this->content.'</div>';
        return $html;
      }          
    }

    // define 'DivFactory' class
    class DivFactory{
      public static function createDiv($type,$content,$id='defaultID',$class='defaultClass'){
        if($type!='AbsoluteDivElement'&&$type!
    ='LeftFloatedDivElement'&&$type!='RightFloatedDivElement'){
          throw new Exception('Invalid object name for being
    created.');
        }
        return new $type($content,$id,$class);
      }
    }

    // use 'DivFactory' class to spawn some div objects - factory
    method is called statically, therefore no factory class instance
    is created
    try{
      $divs=array(DivFactory::createDiv('AbsoluteDivElement','This is
    the content of the absolutely-positioned DIV
    element'),DivFactory::createDiv('LeftFloatedDivElement','This is
    the content of the left-floated DIV
    element'),DivFactory::createDiv('RightFloatedDivElement','This is
    the content of the right-floated DIV element'));
      // display divs elements on the browser
      foreach($divs as $div){
        echo $div->display();
      }
    }
    catch(Exception $e){
      echo $e->getMessage();
      exit();
    }

    After analyzing the respective signatures of all the classes listed above, possibly you'll agree with me that using a static method to create DIV objects (or others, of course) is indeed a very effective process, since it allows the spawning of the mentioned objects without having to deal with an instance of the factory class that built them.

    So far, so good. Now that you surely recalled all of the hands-on examples developed in the previous article of the series, it's time to continue exploring the benefits of working with static data in PHP 5. Therefore, in the next section I'll explain how to create some PHP classes that use static properties, so you can get a better grasp of the idea that stands behind using static members with PHP 5.

    To see how these brand new classes will be built, please click on the link that appears below and keep reading.



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