PHP
  Home arrow PHP arrow Page 5 - 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 - Putting all the classes to work together
    ( Page 5 of 5 )

    If you're anything like me, then probably you may want to see how the bunch of classes that I built during the previous sections can be put to work conjunctly to implement the command pattern. Therefore, in response to this concrete requirement, below I set up a comprehensive example which demonstrates the driving logic of the pattern. Please, have a look at the code sample show below:

    try{        // instantiate 'DataCommanded' object        $dataCommanded=new DataCommanded('This is a test string.');        // display data string after instantiating ' dataCommanded'  object        echo $dataCommanded->getDataString();        /* displays the following        This is a test string.        */        // instantiate 'StringToUpperCommand' object        $strToUp=new StringToUpperCommand($dataCommanded);        // execute 'setUppercasedString()' method        $strToUp->executeCommand();        // displays data string after executing the method        echo $dataCommanded->getDataString();        /*         displays the following:         THIS IS A TEST STRING.         */         // instantiate 'StringToLowerCommand' object         $strToLow=new StringToLowerCommand($dataCommanded);         // execute 'setLowercasedString()' method         $strToLow->executeCommand();         // displays data string after executing the method         echo $dataCommanded->getDataString();         /*         displays the following:                  this is a test string.         */         // instantiate 'StringToReverseCommand' object         $strToRev=new StringToReverseCommand($dataCommanded);         // execute 'setReversedString()' method         $strToRev->executeCommand();         // displays data string after executing the method         echo $dataCommanded->getDataString();         /*         displays the following:         gnirts tset a si siht            */ } catch(Exception $e){        echo $e->getMessage();        exit(); }

    With regard to the example shown above, you can see how the format of the input string passed initially to the "DataCommand" class is affected by the successive commanders, after calling their corresponding "executeCommand()" methods.

    In addition, you should notice that every time one of these methods is invoked by a particular command class, the inputted string is modified internally in the commanded object, since it encapsulates all the logic required for performing these specific tasks.

    Okay, after seeing the command pattern in action, I'm sure you'll agree with me that its implementation is quite easy using PHP, and it actually doesn't require hard work at all. From this point onward, you may want try developing your own examples and applying this pattern in many creative ways. It's really exciting!

    To wrap up

    In this first part of the series, I introduced the basics of applying the command pattern with PHP 5. Hopefully, the hands-on examples that I showed here will be useful enough to get you started on introducing this pattern into your own PHP applications.

    Now, with reference to the contents that I plan to deploy in the second tutorial of the series, I'll continue developing more useful examples concerning the implementation of command objects with PHP. I don't think you'll want to miss it!



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