As I stated in the section that you just read, the last two chainable methods that I'm going to add to the string processor class will be aimed at reversing and replacing their characters respectively. That being said, here's the complete signature of the sample class, this time including the aforementioned methods. Look at it, please: 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; }
// parse special chars of input string // chainable public function parse_chars() { $this->str = htmlentities($this->str); return $this; }
// replace characters in input string // chainable public function replace_char($char = ' ') { $this->str = str_replace($char, '', $this->str); return $this; }
// reverse input string // chainable public function reverse() { $this->str = strrev($this->str); return $this; }
// get input string public function get_string() { return $this->str; } } As you can see, by this time the class implants two additional methods, called "replace_char()" and "reverse()." Logically, the first one is aimed at replacing all of the occurrences of a character of a string with another one, while the second method simply will reverse its content. Below I coded a couple of examples that show how to use these chainable methods. Here's the first one: $strproc = new StringProcessor(' <strong>Hello '. "n" . 'this is a simple example of method chaining in PHP</strong> '); // process string by chaining multiple methods echo $strproc->trim_space()-> remove_tags()-> newline_par()-> replace_char('t')-> get_string(); /* displays the following <p>Hello </p><p>his is a simple example of method chaining in PHP</p> */ And here's the last code sample: echo StringProcessor::factory(' <strong>Hello '. "n" . 'this is a simple example of method chaining in PHP</strong> ')-> trim_space()-> remove_tags()-> reverse()-> get_string(); /* displays the following PHP ni gniniahc dohtem fo elpmaxe elpmis a si siht olleH */ With these two final examples you should be armed with a solid foundation for coding your own chainable methods within your PHP 5 classes. As usual, feel free to edit and improve all of the code samples included in this tutorial, so you can more quickly grasp the logic that stands behind creating truly chainable APIs. Final thoughts Over this third chapter of the series, I completed the definition of this sample string processor class, a process that hopefully helped to reaffirm the concepts that you learned before regarding the definition and implementation of chainable methods in PHP 5. However, this class is just that: an example that can be used as a string helper in the real world. However, it's also possible to apply the method chaining methodology in real applications as well, naturally. Therefore, in the next article I'm going to explain how to implement this approach for building a MySQL abstraction class. Want to see how this will be done? Then don't miss the upcoming tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|