Handling Static Data with PHP 5 - Putting all the classes together (
Page 4 of 4 )
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;
}
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);
}
}
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!