PHP
  Home arrow PHP arrow Page 3 - 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 - Defining some array processing classes
    ( Page 3 of 4 )

    As you'll certainly recall from the prior section, the two concrete factory classes defined in that part of the article were responsible for creating distinct types of array-processing objects, where each of them is backed up by an originating class.

    Therefore, in the new few lines I'm going to list the complete definitions for all of these array handling classes, so you can have a better idea of how they look and work.

    Here are the signatures for these classes:

    // define abstract 'ArrayObj' class
    abstract class ArrayObj{
      
    abstract public function getArraySize();
       abstract public function getArrayElements();
    }

    // define concrete 'UppercasedNumericArrayObj' class
    class UppercasedNumericArrayObj extends ArrayObj{
      
    private $inputArray=array('Element 1','Element 2','Element
    3');
      
    public function getArraySize(){
        
    return count($this->inputArray);
      
    }
      
    public function getArrayElements(){
        
    return array_map(strtoupper,$this->inputArray);
      
    }
    }

    // define concrete 'LowercasedNumericArrayObj' class
    class LowercasedNumericArrayObj extends ArrayObj{
      
    private $inputArray=array('ELEMENT 1','ELEMENT 2','ELEMENT
    3');
      
    public function getArraySize(){
        
    return count($this->inputArray);
      
    }
      
    public function getArrayElements(){
        
    return array_map(strtolower,$this->inputArray);
      
    }
    }

    // define concrete 'UppercasedAssociativecArrayObj' class
    class UppercasedAssociativeArrayObj extends ArrayObj{
      
    private $inputArray=array('Element 1'=>'This is element
    1','Element 2'=>'This is element 2','Element 3'=>'This is element
    3');
      
    public function getArraySize(){
        
    return count($this->inputArray);
      
    }
      
    public function getArrayElements(){
        
    return array_map(strtoupper,$this->inputArray);
      
    }
    }

    // define concrete 'LowercasedAssociativecArrayObj' class
    class LowercasedAssociativeArrayObj extends ArrayObj{
      
    private $inputArray=array('ELEMENT 1'=>'THIS IS ELEMENT
    1','ELEMENT 2'=>'THIS IS ELEMENT 2','ELEMENT 3'=>'THIS IS ELEMENT
    3');
      
    public function getArraySize(){
        
    return count($this->inputArray);
      
    }
      
    public function getArrayElements(){
        
    return array_map(strtolower,$this->inputArray);
      
    }
    }

    As you can see, the group of array processing array classes listed above presents two methods, named "getArraySize()" and "getArrayElements()" respectively, which are common to all of them. However, regardless of the simple logic implemented by these classes, you should notice that the first pair is defined to work in a "numeric" context, while the other two are conceived for use in a "associative" environment.

    Here, it's clear to see how the factory pattern works in a nutshell, since the two factory classes defined in the previous section must return only array processing objects that conform a set of predefined rules to calling code. Now are you starting to grasp the logic that stands behinds the factory pattern? I bet you are!

    All right, having analyzed in detail how all the array handling classes previously created work, it's a good time to develop an example in which all these classes will be put to work in tandem. As you may have guessed, the reason to code this practical example is simply to demonstrate the functionality provided by the pair of factory classes that you learned before.

    Want to see how this example will be set up? Read the following section. I'll be there, waiting for you.



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