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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Handling Static Data with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek