PHP
  Home arrow PHP arrow Page 2 - More Examples of 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

More Examples of Creating Command Objects with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2006-12-19


    Table of Contents:
  • More Examples of Creating Command Objects with PHP 5
  • Building an array command class
  • Creating two more command classes
  • Building an array commanded class
  • Including all the classes in one hands-on example

  • 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


    More Examples of Creating Command Objects with PHP 5 - Building an array command class
    ( Page 2 of 5 )

    Since creating command classes with PHP is indeed a topic that should be addressed by building several hands-on examples, in this section of the tutorial I'll begin defining a set of straightforward classes. These classes will be tasked with instructing a commanded object on how to display the elements of a given input array.

    Logically, this new group of commanders will expose a common method called "executeCommand()." It will be responsible for performing the conversion of the different elements of the inputted array, in this way composing the first building block of the pattern itself. Naturally, the second block will be made up of the corresponding commanded class, which will be properly defined later on.

    For now, I'll start defining the generic structure of an array command class, which looks like this:

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

    If you've read the previous tutorial, then you'll find the signature of the above class quite familiar. Please, notice how this commander accepts an instance of a commanded object as its unique input parameter, and assigns it as a new class property.

    Then, there's the mentioned "executeCommand()" method, which not surprisingly will be charged with instructing the corresponding commanded object on how to display the different elements of a given array. Quite simple, isn't it?

    Now that you hopefully understood how the previous command class was created, let me go one step further and show you the definition of a sub class derived from it. This offers a concrete implementation for the "executeCommand()" method that you saw before.

    That said, the signature of this brand new child class is as follows:

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

    As you can see, the above "ArrayToUpperCommand" class presents a concrete definition for the "executeCommand()" method that was defined as abstract previously. More specifically, this method will use an instance of the commanded object to uppercase all the elements of a given input array. This means that, according to the definition of the command pattern, it should encapsulate all the logic necessary for performing the mentioned array processing.

    So far, the corresponding definitions for the two previous command classes should give you some clear pointers on how to establish a relationship between a commander and a commanded object. However, my intention here is to show you a couple of additional command classes which logically will instruct the commanded object on how to display different elements of a specified array.

    Bearing in mind this possibility for increasing the number of command classes, in the next section I'll create two additional ones. Therefore you can study (and learn) how a single commanded object can accept instructions from multiple commanders.

    The topic sounds really interesting, so go ahead and jump into the next section. I'll be there, waiting for you.



     
     
    >>> 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 6 Hosted by Hostway
    Stay green...Green IT