Home arrow PHP arrow Page 2 - More Examples of Creating Command Objects with PHP 5

Building an array command class - PHP

Are you one of those PHP developers who wants to expand your background in design patterns by learning an additional one? If your answer is affirmative, then this series might be appealing to you. It will teach you, in three educational tutorials, how to create and implement command objects with PHP 5.

TABLE OF CONTENTS:
  1. More Examples of Creating Command Objects with PHP 5
  2. Building an array command class
  3. Creating two more command classes
  4. Building an array commanded class
  5. Including all the classes in one hands-on example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
December 19, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Since creating command classes with PHP is indeed a topic that should be addressed by building several hands-on examples, in this section of the tutorial I'll begin defining a set of straightforward classes. These classes will be tasked with instructing a commanded object on how to display the elements of a given input array.

Logically, this new group of commanders will expose a common method called "executeCommand()." It will be responsible for performing the conversion of the different elements of the inputted array, in this way composing the first building block of the pattern itself. Naturally, the second block will be made up of the corresponding commanded class, which will be properly defined later on.

For now, I'll start defining the generic structure of an array command class, which looks like this:

// define abstract 'ArrayCommand' class (the commander)
abstract class ArrayCommand{
protected $arrayCommanded;
public function __construct($arrayCommanded){
$this->arrayCommanded=$arrayCommanded;
}
abstract public function executeCommand();
}

If you've read the previous tutorial, then you'll find the signature of the above class quite familiar. Please, notice how this commander accepts an instance of a commanded object as its unique input parameter, and assigns it as a new class property.

Then, there's the mentioned "executeCommand()" method, which not surprisingly will be charged with instructing the corresponding commanded object on how to display the different elements of a given array. Quite simple, isn't it?

Now that you hopefully understood how the previous command class was created, let me go one step further and show you the definition of a sub class derived from it. This offers a concrete implementation for the "executeCommand()" method that you saw before.

That said, the signature of this brand new child class is as follows:

// define
concrete 'ArrayToUpperCommand' class (implements concretely the 
'executeCommand()' method class ArrayToUpperCommand extends ArrayCommand{ public function executeCommand(){ $this->arrayCommanded->setUppercasedArray();           } }

As you can see, the above "ArrayToUpperCommand" class presents a concrete definition for the "executeCommand()" method that was defined as abstract previously. More specifically, this method will use an instance of the commanded object to uppercase all the elements of a given input array. This means that, according to the definition of the command pattern, it should encapsulate all the logic necessary for performing the mentioned array processing.

So far, the corresponding definitions for the two previous command classes should give you some clear pointers on how to establish a relationship between a commander and a commanded object. However, my intention here is to show you a couple of additional command classes which logically will instruct the commanded object on how to display different elements of a specified array.

Bearing in mind this possibility for increasing the number of command classes, in the next section I'll create two additional ones. Therefore you can study (and learn) how a single commanded object can accept instructions from multiple commanders.

The topic sounds really interesting, so go ahead and jump into the next 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: