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

       

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek