PHP
  Home arrow PHP arrow Page 5 - 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 - Including all the classes in one hands-on example


    (Page 5 of 5 )

    To show you how all the previously defined classes can be integrated into the same code sample, below I set up a simple yet illustrative script. It shows the neat functionality of the command pattern by using both the commanders and commanded objects that you learned before. Here is the mentioned script, along with its respective outputs:

    try{
    // instantiate 'ArrayCommanded' object
    $arrayCommanded=new ArrayCommanded(array('Element 1',
    'Element 2','Element 3','Element 4','Element 5')); // display input array after instantiating 'ArrayCommanded'
    object print_r($arrayCommanded->getInputArray()); /* displays the following    Array ( [0] => Element 1 [1] => Element 2 [2] => Element 3 [3]
    => Element 4 [4] => Element 5 )            */ // instantiate 'ArrayToUpperCommand' object $arrayToUp=new ArrayToUpperCommand($arrayCommanded); // execute 'setUppercasedArray()' method $arrayToUp->executeCommand(); // displays input array after executing the method print_r($arrayCommanded->getInputArray()); /* displays the following:            Array ( [0] => ELEMENT 1 [1] => ELEMENT 2 [2] => ELEMENT 3 [3]
    => ELEMENT 4 [4] => ELEMENT 5 ) */ // instantiate 'ArrayToLowerCommand' object $arrayToLow=new ArrayToLowerCommand($arrayCommanded); // execute 'setLowercasedArray()' method $arrayToLow->executeCommand(); // displays input array after executing the method print_r($arrayCommanded->getInputArray()); /* displays the following: Array ( [0] => element 1 [1] => element 2 [2] => element 3
    [3] => element 4 [4] => element 5 ) */ // instantiate 'ArrayToReverseCommand' object $arrayToRev=new ArrayToReverseCommand($arrayCommanded); // execute 'setReversedArray()' method $arrayToRev->executeCommand(); // displays data string after executing the method print_r($arrayCommanded->getInputArray()); /* displays the following:         Array ( [0] => element 5 [1] => element 4 [2] => element 3 [3]
    => element 2 [4] => element 1 ) */ } catch(Exception $e){             echo $e->getMessage();             exit(); }

    As you can see, the above example illustrates clearly how the command pattern can be applied by utilizing all the previously created classes. In addition, the prior script also displays the corresponding array elements according to the "array command" applied to them via the respective "ArrayCommanded" class.

    As usual, feel free to tweak the source code of all the classes shown in this article. You can experiment with them and introduce your own approaches for implementing this handy pattern.

    Final thoughts

    That's all for the moment. In this second part of the series, I extended the application of the command pattern by defining some array processing classes, which came in very handy for demonstrating the functionality of the pattern in question.

    However, this journey isn't finished yet. In the last tutorial, I'll show you how to apply the command pattern in a more useful fashion. You'll learn how to use it to work with self-saving objects. I'll meet you there!


    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.

       · 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 2 hosted by Hostway
    Stay green...Green IT