PHP
  Home arrow PHP arrow Page 4 - 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 - Putting all the classes to work together


    (Page 4 of 4 )

    As I expressed in the previous section, below I included a testing script. It shows quite accurately how the two string processor classes that were defined before can be used together to implement the composite pattern.

    In this particular case, the script also shows the corresponding outputs generated by each of the classes, so you can see more clearly how they have similar behaviors.

    Having pointed that out, please take a look at the following code listing:

    try{
       $stringProcessor1=new SingleStringProcessor('This is the first
    sample string and will be used in the composite pattern');
       echo $stringProcessor1->getSelectedStringInfo(1).'<br />';
       /*
       displays the following:
       Value of input string is the following: This is the first
    sample string and will be used in the composite pattern and its
    length is: 73chars.
       */
       $stringProcessor2=new SingleStringProcessor('This is the
    second sample string and will be used in the composite pattern');
       echo $stringProcessor2->getSelectedStringInfo(1).'<br />';
       /*
       displays the following:
       Value of input string is the following: This is the second
    sample string and will be used in the composite pattern and its
    length is: 74chars.
       */
       $stringProcessor3=new SingleStringProcessor('This is the third
    sample string and will be used in the composite pattern');
       echo $stringProcessor3->getSelectedStringInfo(1).'<br />';
       /*
       displays the following:
       Value of input string is the following: This is the third
    sample string and will be used in the composite pattern and its
    length is: 73chars.
       */
       // instantiate 'MultipleStringProcessor' class
       $stringProcessors=new MultipleStringProcessor();
       // add new string processor
       $stringProcessors->addStringProcessor($stringProcessor1);
       echo $stringProcessors->getSelectedStringInfo(
    $stringProcessors->getNumberOfStringProcessors()-1).'<br />';
       /*
       displays the following:
       Value of input string is the following: This is the first
    sample string and will be used in the composite pattern and its
    length is: 73chars.
       */
       // add new string processor
       $stringProcessors->addStringProcessor($stringProcessor2);
       echo $stringProcessors->getSelectedStringInfo(
    $stringProcessors->getNumberOfStringProcessors()-1).'<br />';
       /*
       displays the following:
       Value of input string is the following: This is the second
    sample string and will be used in the composite pattern and its
    length is: 74chars.
       */
       // add new string processor
       $stringProcessors->addStringProcessor($stringProcessor3);
       echo $stringProcessors->getSelectedStringInfo(
    $stringProcessors->getNumberOfStringProcessors()-1).'<br />';
       /*
       displays the following:
       Value of input string is the following: This is the third
    sample string and will be used in the composite pattern and its
    length is: 73chars.
       */
    }

    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    If you take some time and study the outputs generated by each of the string processor classes used in the previous example, you’ll probably see that they produce the same results, even when they have different implementations. Of course, this means in simple terms that one string processor or a group of them expose the same behavior, in this way completing the programmatic model dictated by the composite pattern.

    As usual with all my PHP articles, feel free to modify the source code of the classes shown here, so you can create your own hands-on examples, and eventually acquire a better understanding of how this handy pattern works.

    Final thoughts

    That’s about it. In the two articles of this series, you’ve hopefully expanded your background in pattern-based programming by adding a new pattern to your list. Of course I’m speaking of the composite pattern, which as you saw establishes a programmatic model where one object or a group of them behave similarly.

    See you in the next PHP tutorial!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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