Home arrow PHP arrow 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.

  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



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.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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