Introduction to Creating Command Objects with PHP 5

In this article, the first part of a series, you’ll learn the basics of applying the command pattern with PHP 5. As always, plenty of hands-on examples are included.

If you’ve achieved a considerable background working with PHP, then you’ll agree with me that one of the most exciting aspects of using this server-side scripting language is experimenting with the neat capabilities offered by its Object Model (this is true, particularly with PHP 5). Numerous and useful features have been added to original package to provide developers with more robust and reliable object-based programming tools for developing web applications.

However, the more you explore the advanced capabilities of PHP, the more you want to know about translating to functional code all the theoretical concepts that you learned in your career. Of course, this situation is also applicable to implementing several design patterns, since many of them are rather hard to grasp, at least if you don’t have the chance to see these patterns applied in a concrete case where you can understand more easily how they work.

Driven by the simple motivation of giving you a few useful pointers on how to apply some specific design patterns that may potentially be interesting to you, in this three-part series, I’ll introduce the use of command objects in PHP 5, or what’s known popularly as the command pattern. But before I proceed to show you some concrete code related to the referenced topic, first let me tell you how this pattern works. In the command pattern, one object encloses all the programming logic required for executing a specific method into another one. Sounds pretty simple, doesn’t it?

In this case, the object responsible for encapsulating the logic of a method into another is called the commanded one, while the object that calls the method in question is the commander. Most of the time, the commander exposes an “execute()” method or something similar, which is only responsible for invoking a method of the mentioned commanded. Naturally, this group of buzzwords that I have used will be much easier to grasp when you take a look at the introductory examples that you’ll learn over the course of this series.

Okay, at this stage hopefully the creation and implementation of the command pattern with PHP 5 has already caught your attention, so let’s move on together and start learning more on how this pattern can be applied with a few comprehensible hands-on examples. Let’s go!

{mospagebreak title=Creating a command class}

 As usual, a good place to start explaining how to create command objects with PHP is setting up a comprehensive example, which can demonstrate clearly the driving logic of the pattern in question. Therefore, keeping this condition in mind, I will first define the generic structure of a command class, and then create the corresponding commanded object.

But it’s best to do one thing at time, thus I’ll start creating the mentioned command class, which looks like this:

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

As you can see, in the above example I defined an abstract class named “DataCommand,” which naturally takes up an instance of a commanded object as the unique input parameter and assigns it as a class property. In addition, the abstract “executeCommand()” naturally remains undefined, but it shows in a nutshell how the pattern works. All it has to do is invoke a method of the commanded object, which encapsulates all the required logic for performing a given task. 

Logically, the previous definition will be more easily understood if you take a look at the following sub class, which offers a concrete definition for the previous “executeCommand()” object. Here is the signature for this brand new class; please take a look at it: 

// define concrete 'StringToUpperCommand' class (implements concretely
the 'executeCommand()'method class StringToUpperCommand extends DataCommand{        public function executeCommand(){               $this->dataCommanded->setUppercasedString();                   } }

After examining the definition for the above sub class, you’ll have to agree with me that things are getting really interesting. Please, notice how the respective “executeCommand()” method now performs a specific task, which only consists of upper-casing a given string via the “setUppercasedString()” method. At this point, do you see how the mentioned method houses all the required logic for doing its thing in another object? I hope you do. After all, this is what the command pattern is about!

So far, so good. Now that you know how the corresponding command class looks, it’s time to leap forward and learn how to define some additional command objects. This will expand the range of application for this pattern before we create the respective commanded class.

To see how these new classes will be created, please click on the link below and read the following section.

{mospagebreak title=Creating additional command classes}

As you learned in the previous section, the command class, called “StringToUpperCommand,” was responsible for executing the corresponding “setUppercasedString()” method, in this case provided by the commanded object. This fact demonstrates, in a clear way, the structure of the command pattern, since the method I mentioned previously houses all the logic required to be called into the pertinent command class.

However, as you saw previously, the command class that I just defined is capable of instructing the commanded object to uppercase a specified input string. Therefore, and considering the intrinsic versatility of the command pattern, I’m going to create a few additional command classes, so that you can understand how one single commanded object can receive “orders” from different commanders. That would definitely be interesting, right?

Okay, that being said, below I coded a couple of extra command classes. These classes are tasked with upper-casing and reversing a given string in the commanded object via their corresponding “executeCommand()” methods. The signatures for these brand new classes are as follows:

// define concrete 'StringToLowerCommand' class (implements concretely
the 'executeCommand()'method class StringToLowerCommand extends DataCommand{        public function executeCommand(){                $this->dataCommanded->setLowercasedString();                   } } // define concrete 'StringToReverseCommand' class (implements concretely
the 'executeCommand()'method class StringToReverseCommand extends DataCommand{       public function executeCommand(){                $this->dataCommanded->setReversedString();          } }

As you’ll possibly realize, the two command classes defined above are very similar to the first one that I coded in the previous section. Naturally, the only difference to spot here is the implementation of each of their “executeCommand()” methods, which are responsible for upper-casing and reversing a given input string into the corresponding $dataCommanded object.

At this point, and after defining the three previous command classes, it’s time to move forward and learn how to create the long awaited commanded class, since I haven’t shown how it will look.

In the next few lines I’ll demonstrate how to define this relevant class, therefore if you’re interested in learning how this process will be achieved, click on the link that appears below and keep reading.

{mospagebreak title=Defining a commanded class}

In consonance with the concepts that I stated in the section you just read, the next step that I’m going to take consists of creating the corresponding commanded class referenced by the $dataCommanded object used by the different command classes. Hopefully, after showing you the signature for the class in question, you’ll understand more clearly the logic that stands behind the command pattern.

All right, that said, here is the definition for the commanded class, which for this specific case has been called “DataCommanded.” Its signature is as follows:

// define 'DataCommanded'class class DataCommanded{         private $dataString;         public function __construct($dataString){                $this->setDataString($dataString);         }         public function setDataString($dataString){                if(!is_string($dataString)||!$dataString){                       throw new Exception('Input data must be a
non-empty string!');                }                $this->dataString=$dataString;         }         public function getDataString(){                return $this->dataString;         }         // uppercase data string (encapsulates all the logic to execute
the method in the command object)         public function setUppercasedString(){                $this->setDataString(strtoupper($this->getDataString()));         }         // lowercase data string (encapsulates all the logic to execute
the method in the command object)         public function setLowercasedString(){                $this->setDataString(strtolower($this->getDataString()));         }         // reverse data string (encapsulates all the logic to execute
the method in the command object)         public function setReversedString(){                $this->setDataString(strrev($this->getDataString()));         } }

In this case, and after examining the structure of the above commanded class, I’m certain that you’ll grasp the meaning that surrounds the implementation of the command pattern. As you can see, this new class now exposes three concrete methods, called “setUppercasedString(),” “setLowercasedString()” and “setReversedString()” respectively. According to the definition for the command pattern that you read in the beginning of this article, these classes are capable of encapsulating all the logic required for performing a given task into another object, in this case represented by the set of commanders.

In this particular example, these methods are invoked directly by the respective command classes to lowercase, uppercase and reverse the original input string, passed in a parameter to the previous “DataCommanded” class. This completes the structure of the pattern in question.

Nevertheless, the group of classes defined before would be rather useless if I don’t show you a concrete hands-on example, where all of them are put to work together. After all, this sounds pretty logical, right?

Considering this situation, in the final section of this first article, I’ll set up a practical example which hopefully will help you to understand how each of the previously created classes fits each other.

Do you want to see how this example will be developed? Keep reading, please.

{mospagebreak title=Putting all the classes to work together}

If you’re anything like me, then probably you may want to see how the bunch of classes that I built during the previous sections can be put to work conjunctly to implement the command pattern. Therefore, in response to this concrete requirement, below I set up a comprehensive example which demonstrates the driving logic of the pattern. Please, have a look at the code sample show below:

try{        // instantiate 'DataCommanded' object        $dataCommanded=new DataCommanded('This is a test string.');        // display data string after instantiating ' dataCommanded'  object        echo $dataCommanded->getDataString();        /* displays the following        This is a test string.        */        // instantiate 'StringToUpperCommand' object        $strToUp=new StringToUpperCommand($dataCommanded);        // execute 'setUppercasedString()' method        $strToUp->executeCommand();        // displays data string after executing the method        echo $dataCommanded->getDataString();        /*         displays the following:         THIS IS A TEST STRING.         */         // instantiate 'StringToLowerCommand' object         $strToLow=new StringToLowerCommand($dataCommanded);         // execute 'setLowercasedString()' method         $strToLow->executeCommand();         // displays data string after executing the method         echo $dataCommanded->getDataString();         /*         displays the following:                  this is a test string.         */         // instantiate 'StringToReverseCommand' object         $strToRev=new StringToReverseCommand($dataCommanded);         // execute 'setReversedString()' method         $strToRev->executeCommand();         // displays data string after executing the method         echo $dataCommanded->getDataString();         /*         displays the following:         gnirts tset a si siht            */ } catch(Exception $e){        echo $e->getMessage();        exit(); }

With regard to the example shown above, you can see how the format of the input string passed initially to the “DataCommand” class is affected by the successive commanders, after calling their corresponding “executeCommand()” methods.

In addition, you should notice that every time one of these methods is invoked by a particular command class, the inputted string is modified internally in the commanded object, since it encapsulates all the logic required for performing these specific tasks.

Okay, after seeing the command pattern in action, I’m sure you’ll agree with me that its implementation is quite easy using PHP, and it actually doesn’t require hard work at all. From this point onward, you may want try developing your own examples and applying this pattern in many creative ways. It’s really exciting!

To wrap up

In this first part of the series, I introduced the basics of applying the command pattern with PHP 5. Hopefully, the hands-on examples that I showed here will be useful enough to get you started on introducing this pattern into your own PHP applications.

Now, with reference to the contents that I plan to deploy in the second tutorial of the series, I’ll continue developing more useful examples concerning the implementation of command objects with PHP. I don’t think you’ll want to miss it!

Google+ Comments

Google+ Comments