PHP
  Home arrow PHP arrow Page 2 - Working with Strings and the Composite Pattern in 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? 
Google.com  
PHP

Working with Strings and the Composite Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    Working with Strings and the Composite Pattern in PHP 5 - Handling strings of data
    ( Page 2 of 4 )

    In consonance with the objective of this tutorial, I’m going to build an abstract class. The class will define a generic model for creating some simple string processor objects.

    Later on, based on the structure provided by this base class, I’m going to derive from it a pair of subclasses. Despite their different implementations, these subclasses will expose the same behavior when used within the context of a given PHP application.

    As you may have noticed, this is exactly the intrinsic definition of the composite pattern. Having said that, please pay attention to the definition of the abstract class, shown below:    

    // define abstract 'StringProcessor' class
    abstract class StringProcessor{
       // get information about selected string
       abstract public function getSelectedStringInfo(
    $singleStringProcessor);
       // get number of string processors
       abstract public function getNumberOfStringProcessors();
       // add new string processor
       abstract public function addStringProcessor(
    $singleStringProcessor);
    }

    As you can see, the above “StringProcessor” class presents a few generic methods that obviously will come in handy for performing some useful tasks. These tasks include getting information about a specific string of data, and counting and adding new string processor objects, which are inputted straight into the class via some of these methods.

    However, don’t worry for the moment about how all these string processors will be defined, since I’ll be demonstrating how they look in the section to come.

    All right, at this stage I simply created the abstract class that you saw before, but maybe you’re wondering…how does this fit into the schema imposed by the composite pattern?

    Good question! Fortunately, the answer is quite simple. Since the composite pattern states clearly that one object or a group of objects will expose the same behavior across a given application, in the next few lines I’m going to create two subclasses from the base “StringProcessor.”

    As you’ll see, these child classes will behave identically, in this way implementing the programmatic model dictated by the aforementioned pattern. Quite simple, isn’t it?

    Okay, at this moment I’m pretty sure that you want to learn how these subclasses will be created, so click on the link that appears below and keep reading.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek