Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Introducing Static Members and Methods in PHP 5
  2. Static members and methods: an example
  3. Implementing the Singleton design pattern
  4. Building an array processing factory
  5. Building an array processor factory continued
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
October 02, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> 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: