PHP
  Home arrow PHP arrow Page 4 - 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 - Building an array commanded class
    ( Page 4 of 5 )

    To complete the adequate implementation of the command pattern, I need to define another class. This one will be called "ArrayCommanded." According to the definition of the pattern in question, it will be responsible for encapsulating into different methods all the logic required for manipulating the format of the elements that correspond to an inputted array.

    Does this sound a bit confusing? Fear not, because it isn't. Please, take a look at the signature for this new class to clarify your possible questions:

    // define 'ArrayCommanded' class 
    
    class ArrayCommanded{
    
    	private $inputArray;
    
    	public function __construct($inputArray){
    		
    		$this->setInputArray($inputArray);
    	}
    
    	public function setInputArray($inputArray){
    
    		if(!is_array($inputArray)||count($inputArray)<1){
    
    			throw new Exception('Input data must be a 
    non-empty array!');                         }                         $this->inputArray=$inputArray; } public function getinputArray(){ return $this->inputArray; } // uppercase input array (encapsulates all the logic to execute
    the method in the command object) public function setUppercasedArray(){ $this->setinputArray(array_map('strtoupper',$this->
    getInputArray())); } // lowercase input array (encapsulates all the logic to execute
    the method in the command object) public function setLowercasedArray(){ $this->setinputArray(array_map('strtolower',
    $this->getInputArray())); } // reverse input array (encapsulates all the logic to execute
    the method in the command object) public function setReversedArray(){ $this->setinputArray(array_reverse($this->
    getInputArray())); } }

    As I said previously, the above "ArrayCommanded" class presents all the required methods. This is useful for processing the format that corresponds to the elements of the "$inputArray" array. Even when the logic implemented by the referenced methods is very easy to follow, it demonstrates in a clear fashion how a single commanded class can be constructed in such a way that it can accept instructions from different commanders. Isn't this great?

    All right, now that you've learned how the commanders were appropriately created, in conjunction with defining their corresponding commanded class, it's time to move forward and tackle the last section of this article. I will show you how all these classes can work together; in short, you'll see the commander pattern in action.



     
     
    >>> 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 3 Hosted by Hostway
    Stay green...Green IT