PHP
  Home arrow PHP arrow Page 4 - 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 
IBM Rational Software Development Conference
 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
     
     
    Actuate
     
    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 - 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


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