Home arrow PHP arrow Page 2 - Completing a Sample String Processor with Method Chaining

Review: the string processor class - PHP

In this third part of a 12-part series on method chaining, I complete the definition of the sample string processor class. This process will help reaffirm the concepts that you learned before regarding the definition and implementation of chainable methods in PHP 5.

TABLE OF CONTENTS:
  1. Completing a Sample String Processor with Method Chaining
  2. Review: the string processor class
  3. Replacing characters of a string with HTML entities
  4. Replacing and reversing characters of a literal
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
October 28, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before I proceed to add a few more chainable methods to the string processor class to extend its existing functionality, it'd be really useful to recall its current signature, as defined in the previous article of the series.

That being said, here's the definition of this sample class, which demonstrates in a nutshell how to code chainable methods in PHP 5:

class StringProcessor

{

private $str = '';

private static $instance = NULL;

 

// factory method

// returns a singleton of string processor

// chainable

public static function factory($str)

{

if (self::$instance === NULL)

{

self::$instance = new StringProcessor($str);

}

return self::$instance;

}

 

// constructor

public function __construct($str = 'This is a default string')

{

$this->str= $str;

}

 

// trim white space from input string

// chainable

public function trim_space()

{

$this->str = trim($this->str);

return $this;

}

 

// uppercase input string

// chainable

public function str_upper()

{

$this->str = strtoupper($this->str);

return $this;

}

 

// lowercase input string

// chainable

public function str_lower()

{

$this->str = strtolower($this->str);

return $this;

}

 

// remove eventual HTML and PHP tags from input string

// chainable

public function remove_tags()

{

$this->str = strip_tags($this->str);

return $this;

}

 

// convert new lines to <br /> tags

// chainable

public function newline_br()

{

$this->str = nl2br($this->str);

return $this;

}

 

// convert new lines to paragraphs

// chainable

public function newline_par()

{

$this->str = '<p>' . str_replace("n", '</p><p>', $this->str) . '</p>';

return $this;

}

 

// get input string

public function get_string()

{

return $this->str;

}

}

Now that you're hopefully familiar with the signature of the above "StringProcessor" class, here's another code sample that shows how to use it to apply some of its filter to a trivial string. Take a look at it:

// process string by using factory method and chaining other multiple methods

echo StringProcessor::factory(' <strong>Hello '. "n" . 'this is a simple example of method chaining in PHP</strong> ')->

trim_space()->

remove_tags()->

newline_par()->

get_string();

/*

displays the following

<p>Hello </p><p>this is a simple example of method chaining in PHP</p>

*/

As you can see, in this particular case I decided to utilize the class's "factory()" method to process an example string, which not only can be chained to other methods very easily, but returns only a singleton of the class. Pretty illustrative and educational, right?

Well, at this point I'm sure you recalled the way that this simple class was coded, so it's time to continue extending its current functionality. Thus, in accordance with the concepts deployed in the introduction, in the following section I'm going to add to the class a whole new chainable method that will be tasked with replacing certain characters of an incoming string with their corresponding HTML entities.

To learn more about how this specific method will be implemented, click on the link below and keep reading.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: