Home arrow PHP arrow Page 5 - Introduction to Creating Command Objects with PHP 5

Putting all the classes to work together - PHP

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.

TABLE OF CONTENTS:
  1. Introduction to Creating Command Objects with PHP 5
  2. Creating a command class
  3. Creating additional command classes
  4. Defining a commanded class
  5. Putting all the classes to work together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 14
December 12, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



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