PHP
  Home arrow PHP arrow Page 4 - The Basics of Using the Factory Patter...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: 4 stars4 stars4 stars4 stars4 stars / 8
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Indeed, in the terrain of pattern-based programming, certainly the factory pattern...
     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway