It's pretty common when using PHP to develop dynamic, object-oriented applications. Every so often, though, you need to work with static data. This article will explain how to work with static data and static properties, and show you how this ability can be useful in real-world situations.
As I promised in the section that you just read, below are the respective definitions for all the sample classes that I created here, so you can have them at your disposal if you may want to use them to improve your existing skills for using static methods with PHP 5.
That being said, here are the aforementioned classes:
// 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; }
// 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); } }
Final thoughts
As you saw, in this first tutorial of the series I demonstrated how to use static methods in PHP 5 to perform a pretty useful task -- creating DIVs to be displayed on a given web page. Naturally, this concept can be easily extended to other PHP applications that require the implementation of the factory pattern as well, so all the examples shown here should be educational enough to point you in the right direction.
In the next part of the series, I'll be covering another important topic surrounding the use of static data with PHP 5, more specifically concerning the utilization of static properties in concrete situations.
Now that you know what the next part will cover, you won't want to miss it!