Home arrow PHP arrow Page 3 - More Examples of Creating Command Objects with PHP 5

Creating two more command classes - PHP

Are you one of those PHP developers who wants to expand your background in design patterns by learning an additional one? If your answer is affirmative, then this series might be appealing to you. It will teach you, in three educational tutorials, how to create and implement command objects with PHP 5.

  1. More Examples of Creating Command Objects with PHP 5
  2. Building an array command class
  3. Creating two more command classes
  4. Building an array commanded class
  5. Including all the classes in one hands-on example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
December 19, 2006

print this article



As I stated in the previous section, I'd like to extend the initial example by creating two additional command classes. These classes will control programmatically by different methods the way that elements of a specified array are displayed. Keeping in mind this condition, in the following code sample I defined the "ArrayToLowerCommand" and "ArrayToReverse" classes respectively, which not surprisingly will turn into upper cased strings all the elements of an input array, in addition to reversing these elements as well.

Please, take a look at the respective signatures for these new pair of classes:

// define concrete 'ArrayToLowerCommand' class (implements concretely 
the 'executeCommand()' method class ArrayToLowerCommand extends ArrayCommand{ public function executeCommand(){ $this->arrayCommanded->setLowercasedArray();           } } // define concrete 'ArrayToReverseCommand' class (implements concretely
the 'executeCommand()' method class ArrayToReverseCommand extends ArrayCommand{ public function executeCommand(){ $this->arrayCommanded->setReversedArray();    } }

In this case, the two new command classes listed above present signatures that are nearly identical to the previously defined "ArrayToUpperCommand" class. Of course, the only small difference rests on the definition of their respective "executeCommand()" methods, which perform distinct tasks, even when they're called the same.

Also, it's important to stress a relevant point with reference to all the command classes: notice how they use an instance of the commanded object to perform well-differentiated processes, but in all the cases, the corresponding methods encapsulate the complete logic for doing their business only into the mentioned commanders. Are you starting to see how this pattern works now? I bet you are.

Okay, at this stage you hopefully understand the principle that stands behind creating all these array command classes, but... what about defining the respective array commanded object?

Fortunately for you and me, the creation of commanded objects is indeed a no-brainer process that can be done with minor hassles, something that I'm going to show you in the next few lines. Therefore, if you're curious about how these types of objects will be defined, please click on the link below and keep reading.

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