Home arrow PHP arrow Page 3 - Handling Static Data with PHP 5

Factoring DIVs using both conventional and dynamic methods - PHP

It's pretty common when using PHP to develop dynamic, object-oriented applications. Every so often, though, you need to work with static data. This article will explain how to work with static data and static properties, and show you how this ability can be useful in real-world situations.

TABLE OF CONTENTS:
  1. Handling Static Data with PHP 5
  2. Building dynamic web page DIVs
  3. Factoring DIVs using both conventional and dynamic methods
  4. Putting all the classes together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
September 11, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: