PHP
  Home arrow PHP arrow Page 3 - 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 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 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 - Factoring DIVs using both conventional and dynamic methods


    (Page 3 of 4 )

    As you'll certainly recall from the prior section, my intention here is to demonstrate how a static method can be really useful in certain cases, particularly when it's necessary to spawn a determined number of objects, but without having to create an instance of the factory class that builds the objects in question. This concept is perfectly applicable to all of the DIV classes that were defined previously, so let me show you a comparative example where two different factory classes are used to instantiate a few DIV objects, first by using a conventional factoring method, and then by using a static method.

    Having said that, here is the signature of the first factory class, which in this case defines a conventional factoring method for spawning some DIV objects:

    // define 'DivFactory' class - no static method is used in this
    case
    class DivFactory{

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

    As you can see, the previous class has only one factory method, called "createDiv()", which is responsible for returning to client code a specific type of DIV object. Obviously, the method is callable from inside the object context, which implies that it can be used easily to instantiate some basic DIV objects, as indicated below: 

    try{

      // use 'DivFactory' class to spawn some div objects - no static
    method is used (poor implementation since factory class instance
    is created)
      $divFactory=new DivFactory;
      $divs=array($divFactory->createDiv('AbsoluteDivElement','This
    is the content of the absolutely-positioned DIV
    element'),$divFactory->createDiv('LeftFloatedDivElement','This is
    the content of the left-floated DIV element'),$divFactory-
    >createDiv('RightFloatedDivElement','This is the content of the
    right-floated DIV element'));

      // display DIV elements on the browser
      foreach($divs as $div){
        echo $div->display();
      }
    }
    catch(Exception $e){
      echo $e->getMessage();
      exit();
    }

    If you study the previous code sample, then you'll realize that its implementation is rather inefficient, since it creates an unnecessary instance of the factory class to spawn three different DIV objects, which are finally used to display the real containers on the browser via their "display()" method.

    Quite possibly, at this very moment you're wondering what's wrong with creating an instance of the factory class. Actually, not much, but this approach can be improved if a static factory method is used instead of a conventional one.

    To help you understand this concept, below I redefined the previous factory class in such a way that it now uses a static method to create instances of the three DIV objects that were shown in the prior section.

    The signature of this improved factory class is as follows:

    // define 'DivFactory' class - static method is used
    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);
      }
    }

    As shown above, the factory class looks nearly the same as the one in all the previous examples. Of course, the only difference here is the declaration of its factoring method static. This means that it can be perfectly callable from outside the object context, thus avoiding the unnecessary instantiation of the pertinent factory class. Pretty neat, right?

    To reaffirm this concept, below I listed an example that shows how to spawn three different DIV objects, without using any instance of the factory class in question:

    // use 'DivFactory' class to spawn some div objects - factory
    method is called statically, so no factory class instance is
    created
    try{
      $divs=array(DivFactory::createDiv('AbsoluteDivElement','This is
    the content of the absolutely-positioned DIV
    element'),DivFactory::createDiv('LeftFloatedDivElement','This is
    the content of the left-floated DIV
    element'),DivFactory::createDiv('RightFloatedDivElement','This is
    the content of the right-floated DIV element'));

      // display divs elements on the browser
      foreach($divs as $div){
        echo $div->display();
      }
    }
    catch(Exception $e){
      echo $e->getMessage();
      exit();
    }

    See how useful it is to utilize a static method to create the previous DIV objects? I guess you do! Please, notice how the aforementioned DIVs are properly spawned by calling statically the corresponding "createDiv()" method. In this case, hopefully I provided you with a concrete example where a static method is used in a helpful fashion for implementing the popular factory pattern.

    All right, at this stage I'm sure that you already have an accurate idea of how to take advantage of the remarkable functionality provided by static methods in PHP 5. In the next section of this tutorial I'll be listing the source code of all the classes built here, so you can use them to develop your own testing examples.

    Click on the link below and keep reading, please.

    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

    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - 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...




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