Home arrow PHP arrow Page 4 - Handling Static Data with PHP 5

Putting all the classes together - PHP

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.

TABLE OF CONTENTS:
  1. Handling Static Data with PHP 5
  2. Building dynamic web page DIVs
  3. Factoring DIVs using both conventional and dynamic methods
  4. Putting all the classes together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
September 11, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: