PHP
  Home arrow PHP arrow Page 2 - 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 
VeriSign Whitepapers 
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 - 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


       · 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 6 hosted by Hostway