Home arrow PHP arrow Page 4 - The Basics of Using the Factory Pattern in PHP 5

Testing the functionality of the factory pattern - PHP

If you need to create multiple objects that belong to the same family, you probably want to use the factory pattern. This three-part series takes a close look at using the factory pattern in PHP.

TABLE OF CONTENTS:
  1. The Basics of Using the Factory Pattern in PHP 5
  2. Developing some basic factory classes
  3. Defining some array processing classes
  4. Testing the functionality of the factory pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 13
June 25, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: