HomePHP 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.
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 ) */ }
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!