HomePHP Page 4 - Introducing Static Members and Methods in PHP 5
Building an array processing factory - 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.
If you’ve been reading some of my PHP-related articles, published here on the prestigious Developer Shed network, then you’ll know that I’m a strong advocate of using hands-on examples for reaffirming concepts that are part of the unavoidable theory. For that reason, I'm going to show you another case where static methods and properties can be used together inside the same PHP application.
In short, let me explain the basics of the example that I plan to build: first off, I’ll create a few array handling classes, aimed at processing array elements in different ways. Then, with these classes well underway, I’ll define a generic array processing factory class, which will use a couple of static methods and properties to “fabricate” each of the array handling classes that I mentioned before.
Since this example seems pretty promising, I’ll begin by listing the set of array handling classes that I plan to use here. Below are the respective signatures of these classes:
// define abstract 'ArrayProcessor' class
abstract class ArrayProcessor{
private $arrayFile;
private $elements=array();
abstract function addArrayElement($element);
abstract function processArray();
abstract function saveArray();
abstract function getArray();
}
// define 'ArrayToUppercase' class
class ArrayToUppercase extends ArrayProcessor{
public function__construct($arrayFile=
'default_path/array_data.txt')
{if(!file_exists($arrayFile))
{
throw newException
('Invalid array file!');
}
$this->arrayFile=$arrayFile;
}
public function addArrayElement($element){
if(!$element)
{
throw new Exception
('Invalid array element! Must be a non-empty value.');
}
$this->elements[]=$element;
}
// convert array elements to uppercase
public function processArray(){
$this->elements=array_map
('strtoupper',$this->elements);
}
// save array elements to file
public function saveArray(){
if(!$fp=fopen($this->arrayFile,'a+')){
throw new Exception
('Error opening array file');}
if(!fwrite($fp,serialize($this->elements))){
throw new Exception
('Error writing data to file');}
fclose($fp);
}
// get array of elements
public function getArray(){
return $this->elements;
}
// define 'ArrayToLowercase' class
class ArrayToLowercase extends ArrayProcessor{
public function __construct
($arrayFile='default_path/array_data.txt'){
if(!file_exists($arrayFile)){
throw new Exception
('Invalid array file!');
}
$this->arrayFile=$arrayFile;
}
public function addArrayElement($element){
if(!$element)
throw new Exception
('Invalid array element! Must be a non-empty value.');
}
$this->elements[]=$element;
}
// convert array elements to lowercase
public function processArray(){
$this->elements=array_map
('strtolower',$this->elements);
}
// save array elements to file
public function saveArray(){
if(!$fp=fopen($this->arrayFile,'a+')){
throw new Exception
('Error opening array file');
}
if(!fwrite($fp,serialize($this->elements))){
throw new Exception
('Error writing data to file');
}
fclose($fp);
}
// get array of elements
public function getArray(){
return $this->elements;
}
}
// define 'ArrayToReverse'class
class ArrayToReverse extends ArrayProcessor{
public function __construct
($arrayFile='default_path/array_data.txt')
{
if(!file_exists($arrayFile))
{
throw new Exception
('Invalid array file!');
}
$this->arrayFile=$arrayFile;
}
public function addArrayElement($element){
if(!$element){
throw new Exception
('Invalid array element! Must be a non-empty value.');
}
$this->elements[]=$element;
}
// reverse array elements
public function processArray(){
$this->elements=array_reverse($this->elements);
}
// save array elements to file
public function saveArray(){
if(!$fp=fopen($this->arrayFile,'a+')){
throw new Exception
('Error opening array file');
}
if(!fwrite($fp,serialize($this->elements))){
throw new Exception
('Error writing data to file');
}
fclose($fp);
}
// get array of elements
public function getArray(){
return $this->elements;
}
}
All right, after proceeding further, let me explain briefly what I’ve done until now. As you can see, first I defined an abstract base “ArrayProcessor” class. From this class I derived three different child classes, aimed at performing different operations on the corresponding array elements, such as lowercasing, uppercasing, and finally reversing them.
As you saw, all the classes that were created before have a “save()” method, which allows you to save the corresponding array elements to a given text file. The logic that drives these classes should be extremely easy to grasp.
At this point, and after defining the previous array processing classes, I’m sure you’re wondering... where do static properties and methods fit into this scenario? Well, I’m glad you asked! Because in the following section, I’ll be defining an array processor factory class, which not surprisingly will use the so-called static methods and members.
If you’re interested in learning how this factory class will look, please keep reading.