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