SunQuest
 
       PHP
  Home arrow PHP arrow Page 2 - 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 - Building dynamic web page DIVs


    (Page 2 of 4 )

    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;     
      }

      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;
      }          
    }

    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.

    More PHP Articles
    More By Alejandro Gervasio


       · 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 4 hosted by Hostway