HomePHP Page 5 - Introducing Static Members and Methods in PHP 5
Building an array processor factory continued - PHP
Static properties and methods can be quite useful in a range of situations, not merely for constructing a Singleton class. This article, the first in a two-part series, introduces the basic concepts of static properties and methods, using plenty of hands-on examples.
As I said before, an array processor factory is more than enough to demonstrate how static members and methods can be used in a helpful fashion. But how will this completely new class look?
To answer this question, here’s the definition for the mentioned class:
// define 'ArrayProcessorFactory' class (implements the Singleton pattern)
class ArrayProcessorFactory{
private $allowedArrayProcessors=array
('ArrayToUppercase','ArrayToLowercase','ArrayToReverse');
static private $instance=NULL;
private function__construct(){}
// get instance of array factory
static public function getInstance(){
if(self::$instance==NULL)
{
self::$instance=new ArrayProcessorFactory();
}
return self::$instance;
}
// create array processors
public function createArrayProcessor($arrayProcessor){
if(!in_array($arrayProcessor,$this->
allowedArrayProcessors)){
throw new Exception
('Invalid name for array processor');
}
return new $arrayProcessor;
}
}
As you can see, the above “ArrayProcessorFactory” class is indeed a factory that creates array processors, in accordance with the input passed to its “createArrayProcessor()” method. In this case, since I want to work only with a single instance of this class, I applied the definition of the Singleton pattern that you learned before to achieve this.
In addition, defining the factory class in this way implies using the static $instance property, along with the respective static “getInstance()” method that you saw in previous examples. Now, do you see how static members and methods play a relevant role in the context of the above factory class? I hope you do!
Okay, once the corresponding array processor factory has been defined, it’s time to see how it can be put to work. With reference to this subject, below I coded a short script that uses all the classes I defined earlier:
try{
// get single instance of 'ArrayProcessorFactory' class
$procFactory=ArrayProcessorFactory::getInstance();
// create 'ArrayToUppercase' object
$arrayUp=$procFactory->createArrayProcessor('ArrayToUppercase');
// create 'ArrayToLowercase' object
$arrayLow=$procFactory->createArrayProcessor('ArrayToLowercase');
// create 'ArrayToReverse' object
$arrayRev=$procFactory->createArrayProcessor('ArrayToReverse');
// add elements to first array
$arrayUp->addArrayElement('This is element 1');
$arrayUp->addArrayElement('This is element 2');
$arrayUp->addArrayElement('This is element 3');
$arrayUp->addArrayElement('This is element 4');
$arrayUp->addArrayElement('This is element 5');
// convert array to uppercase
$arrayUp->processArray();
// save array to file
$arrayUp->saveArray();
// print uppercased array
print_r($arrayUp->getArray());
// add elements to second array
$arrayLow->addArrayElement('This is element 1');
$arrayLow->addArrayElement('This is element 2');
$arrayLow->addArrayElement('This is element 3');
$arrayLow->addArrayElement('This is element 4');
$arrayLow->addArrayElement('This is element 5');
// convert array to lowercase
$arrayLow->processArray();
// save array to file
$arrayLow->saveArray();
// print uppercased array
print_r($arrayLow->getArray());
// add elements to third array
$arrayRev->addArrayElement('This is element 1');
$arrayRev->addArrayElement('This is element 2');
$arrayRev->addArrayElement('This is element 3');
$arrayRev->addArrayElement('This is element 4');
$arrayRev->addArrayElement('This is element 5');
// reverse array
$arrayRev->processArray();
// save array to file
$arrayRev->saveArray();
// print uppercased array
print_r($arrayRev->getArray());
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
As shown by the above script, I first used a single instance of the factory class --please examine the implementation of the “getInstance()” method -- in order to create different array processor objects. Then, I populated the pertinent arrays with some trivial values, which were processed and saved appropriately via the corresponding “processArray()” and “save()” methods respectively, and finally the results were outputted to the browser as follows:
Array ( [0] => THIS IS ELEMENT 1 [1] => THIS IS ELEMENT 2 [2] => THIS IS ELEMENT 3 [3] => THIS IS ELEMENT 4 [4] => THIS IS ELEMENT 5 ) Array ( [0] => this is element 1 [1] => this is element 2 [2] => this is element 3 [3] => this is element 4 [4] => this is element 5 ) Array ( [0] => This is element 5 [1] => This is element 4 [2] => This is element 3 [3] => This is element 2 [4] => This is element 1 )
The output shown above clearly demonstrates how both static methods and properties can be used conjunctly to apply, in the correct sequence, the Singleton and Factory patterns. As usual, feel free to tweak all code samples listed here, so you can acquire a better grounding in using static members and methods with PHP.
Bottom Line
In this first part of the series, you learned the basics of defining static members and properties with PHP 5, and used them in concrete cases, such as the implementation of the Singleton and Factory patterns respectively.
However this instructive journey has just started. That’s why in the next tutorial, I’ll show you how to expand the application of static methods in PHP 5, demonstrating how they can be used for interacting with MySQL.
If you wish to find out how this will be done, don’t miss the next part!