PHP
  Home arrow PHP arrow Page 4 - The Basics of Using the Factory Pattern in 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? 
PHP

The Basics of Using the Factory Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2007-06-25


    Table of Contents:
  • The Basics of Using the Factory Pattern in PHP 5
  • Developing some basic factory classes
  • Defining some array processing classes
  • Testing the functionality of the factory pattern

  • 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


    The Basics of Using the Factory Pattern in PHP 5 - Testing the functionality of the factory pattern
    ( Page 4 of 4 )

    In consonance with the concepts deployed in the previous section, my intention here is to illustrate how the factory pattern functions. Below I listed the definition of a simple script that shows how to use the two factory classes defined earlier to create a bunch of array processing objects. Also, it should be noticed that the factories' methods are called statically via the scope resolution operator (::), so no instances of them are required.

    That being said, here is the testing script in question:

    try{
      
    // create lowercased numeric array object
      
    $lowerNumArray=NumericArrayFactory::createArrayObj
    ('lowercase');
      
    // display array object size
      
    echo 'Number of elements of lowercased numeric array is the
    following : '.$lowerNumArray->getArraySize();

       /*
      
    displays the following:
      
    Number of elements of lowercased numeric array is the
    following : 3
      
    */

       // display array object elements
      
    print_r($lowerNumArray->getArrayElements());

       /*
      
    displays the following
      
    Array ( [0] => element 1 [1] => element 2 [2] => element 3 )
      
    */

       // create uppercased numeric array object
      
    $upperNumArray=NumericArrayFactory::createArrayObj
    ('uppercase');
      
    // display array object size
      
    echo 'Number of elements of uppercased numeric array is the
    following : '.$upperNumArray->getArraySize();

       /*
      
    displays the following:
      
    Number of elements of uppercased numeric array is the
    following : 3
      
    */

       // display array object elements
      
    print_r($upperNumArray->getArrayElements());

       /*
      
    displays the following
      
    Array ( [0] => ELEMENT 1 [1] => ELEMENT 2 [2] => ELEMENT 3 )
      
    */

       // create lowercased associative array object
      
    $lowerAssocArray=AssociativeArrayFactory::createArrayObj
    ('lowercase');
      
    // display array object size
      
    echo 'Number of elements of lowercased associative array is
    the following : '.$lowerAssocArray->getArraySize();

       /*
      
    displays the following:
      
    Number of elements of lowercased associative array is the
    following : 3
      
    */

       // display array object elements
      
    print_r($lowerAssocArray->getArrayElements());

       /*
      
    displays the following
      
    Array ( [ELEMENT 1] => this is element 1 [ELEMENT 2] => this
    is element 2 [ELEMENT 3] => this is element 3 )
      
    */

       // create uppercased associative array object
      
    $upperAssocArray=AssociativeArrayFactory::createArrayObj
    ('uppercase');
      
    // display array object size
      
    echo 'Number of elements of uppercased associative array is
    the following : '.$upperAssocArray->getArraySize();

       /*
      
    displays the following:
      
    Number of elements of uppercased associative array is the
    following : 3
      
    */

       // display array object elements
      
    print_r($upperAssocArray->getArrayElements());

       /*
      
    displays the following
      
    Array ( [Element 1] => THIS IS ELEMENT 1 [Element 2] => THIS
    IS ELEMENT 2 [Element 3] => THIS IS ELEMENT 3 )
      
    */
    }

    catch(Exception $e){
      
    echo $e->getMessage();
      
    exit();
    }

    Do you see how easy it was to spawn different array processing objects using statically the corresponding factory classes? Definitely, this is a clear and simple demonstration of how the factory pattern can be implemented with PHP 5.

    However, the prior example shouldn't stop you from developing your own testing scripts. Doing so will give you a better understanding of the remarkable functionality offered by this design pattern in particular.

    Final thoughts

    In this first installment of the series, I introduced the key points of how to implement the factory pattern with PHP 5. Nevertheless, this educational journey has just begun, since in the next part of the series I'm going to teach you how to use this pattern in a more useful fashion, more specifically to work with file and cookie-processing classes.

    Now that you know what the next article will be about, I hope to see you there!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT