HomePHP Page 4 - Introduction to Creating Command Objects with PHP 5
Defining a commanded class - 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.
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.