A String Builder class for PHP

If you're anything like me you love programming in PHP. You also love C# and have become highly annoyed anytime you go do implement a method built into C# in another language - perhaps PHP or Java - only to find out that language x doesn't have that method.

Well, such a thing happened to me yesterday at work while working on a PHP app I'm writing. I went to use a String Builder to concatenate a metric shit pile of strings together (it's part of a template engine I had to re-write for a program at work. Don't worry, it was in a loop.).

The way the author of the engine had done it was laughable and gross, using a . to concat' the various pieces of a string together. I get sick looking at this these days...

I decided to build a helper class in PHP to give me a String Builder that is functionally identical to C#'s String Builder. Easy. Check this shit out:

PHP
  1. /**
  2.  * String Builder class
  3.  * Functionally the same as the one in C#
  4.  * @author Sean Boyer
  5.  */
  6. class StringBuilder {
  7.    
  8.     /**
  9.      * Guess what? This is the string collector.
  10.      * @var string
  11.      */
  12.     private $_string = '';
  13.  
  14.     /**
  15.      * Appends this string onto the end of the full string
  16.      * @param string $string The string to add to the collection
  17.      */
  18.     public function append($string)
  19.     {
  20.         $this->_string .= $string;
  21.     }
  22.  
  23.     /**
  24.      * Appends this string onto the end of the full string, with a line break
  25.      * @param string $string The string to add to the collection. Adds a line break.
  26.      */
  27.     public function appendLine($string)
  28.     {
  29.         $this->_string .= $string . "rn";
  30.     }
  31.  
  32.     /**
  33.      * Returns the full string
  34.      * @param none
  35.      * @return the fully appended string
  36.      */
  37.     public function toString()
  38.     {
  39.         return $this->_string;
  40.     }
  41. }


This is about as simple as it gets, but cleans up your code quite nicely. Anyone could have written this class, but I guess it was me. I didn't even google for this one it was so simple to put together. There are likely (hopefully) dozens of them out on the United iWebz.

Need proof that using this class is a good idea?

Here's an example of jamming some strings together without using a string builder:
PHP
  1. $string = "I am a " . $var1 . " real boy." . "rn";


Versus:
PHP
  1. $builder = new StringBuilder();
  2. $builder->append("I am a");
  3. $builder->append($var1);
  4. $builder->appendLine(" real boy");
  5.  
  6. return $builder->toString();


Yes it's a little bit more typing, but you can see how in a loop, or in a situation where, for god knows what reason, you're jamming 45 hard coded strings together in your code, it keeps things clean.

The appendLine() method rules as it will automatically add a line break for you. Write overloaded methods to get different line breaks if needed. Or for html
tags could be used instead of a linefeed.

This class should be safe for PHP > 4.x. If it's not, upgrade already foo!

It's so much fun to talk about String Builders.