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.
blog comments powered by Disqus |
|
|
|
|
|
|
|