PHP
  Home arrow PHP arrow Page 5 - 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
     
     
     
    ADVERTISEMENT

    Avoid common pitfalls of incorporating spreadsheets into Java apps. Read about it in the free white paper: “Five Biggest Blunders when Building Spreadsheet Applications in JavaDownload Now!

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 4 hosted by Hostway