PHP
  Home arrow PHP arrow Page 2 - Introduction to Creating Command Objects with PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Introduction to Creating Command Objects with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 13
    2006-12-12


    Table of Contents:
  • Introduction to Creating Command Objects with PHP 5
  • Creating a command class
  • Creating additional command classes
  • Defining a commanded class
  • Putting all the classes to work together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Introduction to Creating Command Objects with PHP 5 - Creating a command class
    ( Page 2 of 5 )

     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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT