HomePHP Page 2 - Understanding Static Properties with PHP 5
Reintroducing a previous hands-on example - PHP
The powerful object model offered by PHP 5 presents some useful features that are quite frequently underestimated by the average PHP developer. This is precisely the case with using static methods and properties in conjunction with the object-oriented paradigm, thus if you're interested in learning how to put static data to work for you, then this series of articles might be what you're looking for.
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.