PHP
  Home arrow PHP arrow Page 4 - Handling Static Data with PHP 5
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Handling Static Data with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2007-09-11

    Table of Contents:
  • Handling Static Data with PHP 5
  • Building dynamic web page DIVs
  • Factoring DIVs using both conventional and dynamic methods
  • Putting all the classes together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This first article of the series will explain how to work with static methods in PHP...
     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway