PHP
  Home arrow PHP arrow Page 4 - 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 - Defining a commanded class
    ( Page 4 of 5 )

    In consonance with the concepts that I stated in the section you just read, the next step that I'm going to take consists of creating the corresponding commanded class referenced by the $dataCommanded object used by the different command classes. Hopefully, after showing you the signature for the class in question, you'll understand more clearly the logic that stands behind the command pattern.

    All right, that said, here is the definition for the commanded class, which for this specific case has been called "DataCommanded." Its signature is as follows:

    // define 'DataCommanded'class class DataCommanded{         private $dataString;         public function __construct($dataString){                $this->setDataString($dataString);         }         public function setDataString($dataString){                if(!is_string($dataString)||!$dataString){                       throw new Exception('Input data must be a
    non-empty string!');                }                $this->dataString=$dataString;         }         public function getDataString(){                return $this->dataString;         }         // uppercase data string (encapsulates all the logic to execute
    the method in the command object)         public function setUppercasedString(){                $this->setDataString(strtoupper($this->getDataString()));         }         // lowercase data string (encapsulates all the logic to execute
    the method in the command object)         public function setLowercasedString(){                $this->setDataString(strtolower($this->getDataString()));         }         // reverse data string (encapsulates all the logic to execute
    the method in the command object)         public function setReversedString(){                $this->setDataString(strrev($this->getDataString()));         } }

    In this case, and after examining the structure of the above commanded class, I'm certain that you'll grasp the meaning that surrounds the implementation of the command pattern. As you can see, this new class now exposes three concrete methods, called "setUppercasedString()," "setLowercasedString()" and "setReversedString()" respectively. According to the definition for the command pattern that you read in the beginning of this article, these classes are capable of encapsulating all the logic required for performing a given task into another object, in this case represented by the set of commanders.

    In this particular example, these methods are invoked directly by the respective command classes to lowercase, uppercase and reverse the original input string, passed in a parameter to the previous "DataCommanded" class. This completes the structure of the pattern in question.

    Nevertheless, the group of classes defined before would be rather useless if I don't show you a concrete hands-on example, where all of them are put to work together. After all, this sounds pretty logical, right?

    Considering this situation, in the final section of this first article, I'll set up a practical example which hopefully will help you to understand how each of the previously created classes fits each other.

    Do you want to see how this example will be developed? Keep reading, please.



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