HomePHP 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.
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.