Home arrow PHP arrow Page 3 - Working with Strings and the Composite Pattern in PHP 5

Defining the SingleStringProcessor and MultipleStringProcessor classes - PHP

Are you a PHP developer who wants to improve your skills in pattern-based programming in PHP? If the answer to this question is an emphatic yes, then you should begin reading this article now! Welcome to the final part of the series “Implementing the composite pattern with PHP 5.” Comprised of two instructive tutorials, this series walks you through the basic concepts of this important design pattern, and shows you how to apply it with some educational examples.

  1. Working with Strings and the Composite Pattern in PHP 5
  2. Handling strings of data
  3. Defining the SingleStringProcessor and MultipleStringProcessor classes
  4. Putting all the classes to work together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
March 14, 2007

print this article



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){
       throw new Exception('Invalid input string!');
   public function getSelectedStringInfo($stringProcessor){
       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(){
   public function getSelectedStringInfo($singleStringProcessor){
       return $this->stringProcessors[$singleStringProcessor]-
     return false;
   public function getNumberOfStringProcessors(){
     return count($this->stringProcessors);
   public function addStringProcessor($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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: