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



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