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

Defining some array processing classes - 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

As you'll certainly recall from the prior section, the two concrete factory classes defined in that part of the article were responsible for creating distinct types of array-processing objects, where each of them is backed up by an originating class.

Therefore, in the new few lines I'm going to list the complete definitions for all of these array handling classes, so you can have a better idea of how they look and work.

Here are the signatures for these classes:

// define abstract 'ArrayObj' class
abstract class ArrayObj{
  
abstract public function getArraySize();
   abstract public function getArrayElements();
}

// define concrete 'UppercasedNumericArrayObj' class
class UppercasedNumericArrayObj extends ArrayObj{
  
private $inputArray=array('Element 1','Element 2','Element
3');
  
public function getArraySize(){
    
return count($this->inputArray);
  
}
  
public function getArrayElements(){
    
return array_map(strtoupper,$this->inputArray);
  
}
}

// define concrete 'LowercasedNumericArrayObj' class
class LowercasedNumericArrayObj extends ArrayObj{
  
private $inputArray=array('ELEMENT 1','ELEMENT 2','ELEMENT
3');
  
public function getArraySize(){
    
return count($this->inputArray);
  
}
  
public function getArrayElements(){
    
return array_map(strtolower,$this->inputArray);
  
}
}

// define concrete 'UppercasedAssociativecArrayObj' class
class UppercasedAssociativeArrayObj extends ArrayObj{
  
private $inputArray=array('Element 1'=>'This is element
1','Element 2'=>'This is element 2','Element 3'=>'This is element
3');
  
public function getArraySize(){
    
return count($this->inputArray);
  
}
  
public function getArrayElements(){
    
return array_map(strtoupper,$this->inputArray);
  
}
}

// define concrete 'LowercasedAssociativecArrayObj' class
class LowercasedAssociativeArrayObj extends ArrayObj{
  
private $inputArray=array('ELEMENT 1'=>'THIS IS ELEMENT
1','ELEMENT 2'=>'THIS IS ELEMENT 2','ELEMENT 3'=>'THIS IS ELEMENT
3');
  
public function getArraySize(){
    
return count($this->inputArray);
  
}
  
public function getArrayElements(){
    
return array_map(strtolower,$this->inputArray);
  
}
}

As you can see, the group of array processing array classes listed above presents two methods, named "getArraySize()" and "getArrayElements()" respectively, which are common to all of them. However, regardless of the simple logic implemented by these classes, you should notice that the first pair is defined to work in a "numeric" context, while the other two are conceived for use in a "associative" environment.

Here, it's clear to see how the factory pattern works in a nutshell, since the two factory classes defined in the previous section must return only array processing objects that conform a set of predefined rules to calling code. Now are you starting to grasp the logic that stands behinds the factory pattern? I bet you are!

All right, having analyzed in detail how all the array handling classes previously created work, it's a good time to develop an example in which all these classes will be put to work in tandem. As you may have guessed, the reason to code this practical example is simply to demonstrate the functionality provided by the pair of factory classes that you learned before.

Want to see how this example will be set up? Read the following section. I'll be there, waiting for you.



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