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.
A good place to start demonstrating the remarkable functionality of working with static class methods in PHP 5 is with developing a basic web application. This application's primary goal is to display on the browser a certain number of dynamic DIVs as part of a simple web page.
In this situation, since the number of DIVs that need to be built across different web documents may vary, it would be highly desirable to define a class that spawns as many DIVs as required via the implementation of a static factoring method. But I'm getting ahead of myself, so first let me show you the signatures of the PHP classes that return to client code, the appropriate (X)HTML markup required to display a basic DIV.
Having explained that, these DIV rendering classes look like this:
// 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; } }
As you can see, the business logic implemented by the above PHP classes is very easy to follow. First I built an abstract "DivElement" class for defining the generic structure of a DIV container, and then derived from it three concrete classes. These new classes are tasked with returning to client code the required markup to display an absolutely-positioned DIV, along with others that are left and right-floated respectively.
So far, so good, right? At this stage I defined a few simple classes that display DIV elements on a given web page. The question that comes up now is the following: how can a static method be used in a useful fashion in conjunction with all of these recently created classes?
Well, as I expressed earlier, the best way to take advantage of the functionality encapsulated within the previous DIV classes rests on defining a factory mechanism, which allows the spawning of as many DIVs as required. Thus, here's where the implementation of a static method comes in, since the factory will use a method of this type to create a certain number of DIV objects.
However, all of these interesting tasks will be carried out in the following section, thus click on the link that appears below and keep reading to learn more.