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

Creating a command 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



 As usual, a good place to start explaining how to create command objects with PHP is setting up a comprehensive example, which can demonstrate clearly the driving logic of the pattern in question. Therefore, keeping this condition in mind, I will first define the generic structure of a command class, and then create the corresponding commanded object.

But it's best to do one thing at time, thus I'll start creating the mentioned command class, which looks like this:

// define abstract 'DataCommand' class (the commander) abstract class DataCommand{         protected $dataCommanded;         public function __construct($dataCommanded){                $this->dataCommanded=$dataCommanded;         }         abstract public function executeCommand(); }

As you can see, in the above example I defined an abstract class named "DataCommand," which naturally takes up an instance of a commanded object as the unique input parameter and assigns it as a class property. In addition, the abstract "executeCommand()" naturally remains undefined, but it shows in a nutshell how the pattern works. All it has to do is invoke a method of the commanded object, which encapsulates all the required logic for performing a given task. 

Logically, the previous definition will be more easily understood if you take a look at the following sub class, which offers a concrete definition for the previous "executeCommand()" object. Here is the signature for this brand new class; please take a look at it: 

// define concrete 'StringToUpperCommand' class (implements concretely
the 'executeCommand()'method class StringToUpperCommand extends DataCommand{        public function executeCommand(){               $this->dataCommanded->setUppercasedString();                   } }

After examining the definition for the above sub class, you'll have to agree with me that things are getting really interesting. Please, notice how the respective "executeCommand()" method now performs a specific task, which only consists of upper-casing a given string via the "setUppercasedString()" method. At this point, do you see how the mentioned method houses all the required logic for doing its thing in another object? I hope you do. After all, this is what the command pattern is about!

So far, so good. Now that you know how the corresponding command class looks, it's time to leap forward and learn how to define some additional command objects. This will expand the range of application for this pattern before we create the respective commanded class.

To see how these new classes will be created, please click on the link below and read the following section.

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