PHP
  Home arrow PHP arrow Page 2 - Introduction to Creating Command Objec...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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
     
     
    Actuate
     
    ADVERTISEMENT

    Learn four approaches for automating Excel logic, along with advantages and disadvantages of each. Read all about it in the free whitepaper: “Tapping into Excel Logic from Java: Four Server-Based AlternativesDownload Now!

    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


       · In this first article of the series, you'll learn the key points of applying the...
       · This is a good example of the Command pattern in PHP, i'm not sure why you've moved...
       · Hi James,Thank you for commenting on my PHP article. With reference to your...
     

       

    PHP ARTICLES

    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway