PHP
  Home arrow PHP arrow Page 2 - More Examples of Creating Command Obje...
Administration  
AJAX  
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 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      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
     
     
    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


       · Over the course of this second part of the series, you'll learn how to use command...
       · It's again clear what it does and how it does it, but it's not clear "why" I would...
       · Thank you for posting your comments here. Concerning your question, it doesn’t mean...
       · I trust that there are occasions where it could be usefull. I'd love to see a...
       · Thank you again for commenting here. I’ll keep in mind your suggestion on the...
       · A nice article and good explanation of how the command pattern works. I do agree...
       · Hi friend,Thank you for the kind comments about my article on the command...
       · I had the same exact question - why and when do you really have an advantage of...
       · Reading the first page of this article made me dizzy. I felt like I was reading one...
       · Hello Nikolai,Thank you for posting your questions here. As I said before, this...
       · Hello there,Thank you for commenting my PHP article, and certainly I accept your...
       · Thanks AlejandroA good working example of the described pattern and conveyed...
       · Hi Jon,Thank you for your positive comments on my PHP article. I really...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT