PHP
  Home arrow PHP arrow Page 3 - Working with Strings and the Composite...
Dev Shed Forums 
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

Working with Strings and the Composite Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-03-14

    Table of Contents:
  • Working with Strings and the Composite Pattern in PHP 5
  • Handling strings of data
  • Defining the SingleStringProcessor and MultipleStringProcessor classes
  • 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
     
     
    ADVERTISEMENT


    Working with Strings and the Composite Pattern in PHP 5 - Defining the SingleStringProcessor and MultipleStringProcessor classes


    (Page 3 of 4 )

    To illustrate how the composite pattern can be applied using the abstract string processor class that you learned in the previous section, I'm simply going to derive two subclasses from this base class.

    As you’ll see in a moment, these child classes, which are also string processor objects, will be capable of behaving nearly the same, even when they offer different implementations. Indeed, this sounds like I’m sticking to the model imposed by the composite pattern, right?

    All right, now that you know how these two subclasses are going to work, please have a look at their respective signatures, which are listed below. Here they are:

    // define concrete 'SingleStringProcessor' class
    class SingleStringProcessor extends StringProcessor{
       private $inputString;
       public function __construct($inputString){
         if(!is_string($inputString)){
           throw new Exception('Invalid input string!');
         }
         $this->inputString=$inputString;
       }
       public function getSelectedStringInfo($stringProcessor){
         if($stringProcessor==1){
           return 'Value of input string is the following: '.$this-
    >inputString.' and its length is: '.strlen($this->inputString). 'chars.';
         }
       }
       public function getNumberOfStringProcessors(){
         return 1;
       }
       public function addStringProcessor($singleStringProcessor){
         return false;
       }
    }

    // define concrete 'MultipleStringProcessor' class
    class MultipleStringProcessor extends StringProcessor{
       private $stringProcessors;
       public function __construct(){
         $this->stringProcessors=array();
       }
       public function getSelectedStringInfo($singleStringProcessor){
         if($singleStringProcessor<=count($this->stringProcessors)){
           return $this->stringProcessors[$singleStringProcessor]-
    >getSelectedStringInfo(1);
         }
         return false;
       }
       public function getNumberOfStringProcessors(){
         return count($this->stringProcessors);
       }
       public function addStringProcessor($singleStringProcessor){
         $this->stringProcessors[]=$singleStringProcessor;
       }
    }

    As shown above, both the “SingleStringProcessor” and “MultipleStringProcessor” classes implement all the methods originally declared in the respective base class, which is certainly expected when working with abstract classes. However, aside from this characteristic, I’d like you to pay attention to the definition corresponding to these methods. The definition does show in a clear fashion how this pair of subclasses implement the composite pattern.

    Please notice how the methods that belong to the first child class return to calling code a value of 1 or FALSE, since here it’s assumed that this class is only capable of using a single string processor object.

    On the other hand, there’s the “MultipleStringProcessor” class, which is tasked with handling multiple string processor objects. Nevertheless, the most important thing to stress here is the fact that both classes will behave similarly, regardless of whether they handle only one or a group of string processors. After all, implementing the composite pattern by using the two prior child classes wasn’t hard at all!

    So far, I have shown you the respective signatures corresponding to the “SingleStringProcessor” and “MultipleStringProcessor” classes respectively, but now I’m pretty certain that you want to see how they can be included into a simple script to demonstrate that they do present the same behavior.

    Indeed, the experience sounds interesting, thus in the last section of this tutorial I’m going to create an educational example which will use the two child classes that you learned a few lines above.

    To see how the example in question will be developed, please go ahead and read the next few lines. I’ll be there, waiting for you.

    More PHP Articles
    More By Alejandro Gervasio


       · In this second (and final) article of the series, the composite pattern is...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - 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
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT