PHP
  Home arrow PHP arrow Page 2 - 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? 
Google.com  
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 - Developing some basic factory classes
    ( Page 2 of 4 )

    To start demonstrating the neat functionality provided by the factory pattern, I'll define a few basic factory classes whose primary task will consist of returning different types of array objects to client code according to the concrete specifications of a given programming environment.

    As you'll see, these concrete factories will be capable of "factoring" array objects that will work with both numeric and associative indexes. But let's get away from theory for a moment and see how these brand new factory classes look. Their respective signatures are as follows:

    // define abstract 'ArrayFactory' class
    abstract class ArrayFactory{
      
    abstract public function createArrayObj($type);
    }

    // define concrete factory to create numerically-indexed array
    objects
    class NumericArrayFactory extends ArrayFactory{
      
    private $context='numeric';
      
    public function createArrayObj($type){
        
    $arrayObj=NULL;
        
    switch($type){
          
    case "uppercase";
            
    $arrayObj=new UppercasedNumericArrayObj();
             
    break;
          
    case "lowercase";
            
    $arrayObj=new LowercasedNumericArrayObj();
            
    break;
          
    default:
            
    $arrayObj=new LowercasedNumericArrayObj();
             
    break; 
         
    }
         
    return $arrayObj;
       
    }
    }

    // define concrete factory to create associative array objects
    class AssociativeArrayFactory extends ArrayFactory{
      
    private $context='associative';
      
    public function createArrayObj($type){
        
    $arrayObj=NULL;
        
    switch($type){
          
    case "uppercase";
            
    $arrayObj=new UppercasedAssociativeArrayObj();
            
    break;
          
    case "lowercase";
            
    $arrayObj=new LowercasedAssociativeArrayObj();
            
    break;
          
    default:
            
    $arrayObj=new LowercasedAssociativeArrayObj();
            
    break; 
         
    }
        
    return $arrayObj;
       
    }
    }

    As demonstrated above, the previous concrete factory classes (excepting logically the first one, which has been declared abstract) implement the required business logic to create different types of array objects in accordance with the specifications of a given environment.

    In consonance with the previous concept, the first factory class, which is called "NumericArrayFactory," is responsible for spawning three different types of numeric array objects.  All of them are obviously conceived to work in a "numeric" context.

    Now, concerning the definition of the second concrete factory, you can see that this one returns only associative array objects to client code, which logically must conform to the specifications of an "associative" environment.

    Undeniably, after studying the respective signatures of the two previous factory classes, you'll realize that implementing the factory pattern with PHP is actually an easy-to-grasp process that can be performed with minor hassles.

    So far, so good, right? At this stage I demonstrated how to build a couple of concrete factory classes that are tasked with spawning different types of array objects. So what's the next step? Well, as you learned before, these array objects obviously are spawned from a bunch of concrete classes, so in the following section I'm going to show you their respective signatures. You'll see more easily how the different classes used to implement the factory pattern are linked with each other.

    To learn how these array-related classes will be defined, please click on the link below and keep reading.



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