As I said in the introduction, I’d like to extend the existing functionality of the string processor class coded in the previous article by adding some additional methods to it. This will show you how the method chaining approach can be used to accomplish this task in a simple manner. So, with that idea in mind, below I listed an enhanced version of the string processor class. It includes a brand new chainable method called “newline_br()”, which is a wrapper for the native “nl2br()” native PHP function. Here it is: class StringProcessor { private $str = ''; private static $instance = NULL;
// 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; } // get input string public function get_string() { return $this->str; } } As I explained, now the above “StringProcessor” class implements a new chainable method named “newline_br()”, which is a simple proxy for the “nl2br()” PHP function. Of course, the most relevant aspect of this method is the fact that it’s completely chainable, so it can be used in conjunction with others to replace all of the new lines included in a given string by “<br /”> tags. To demonstrate the functionality of this new chainable method, below I listed another code sample that shows how to work with it. Have a look at the example: $strproc = new StringProcessor(' <p>Hello '. "n" . 'this is a simple example of method chaining in PHP</p> '); // process string by chaining multiple methods echo $strproc->trim_space()-> remove_tags()-> newline_br()-> get_string(); /* displays the following Hello <br /> this is a simple example of method chaining in PHP */ That was not rocket science, right? In the previous example the “newline_br()” method has been chained to some existing ones to replace all of the new line characters included in a sample string by “<br />” tags. The output generated by this particular example demonstrates how compact and tight the code looks when all of these methods are chained in one single step. So far, so good. At this stage hopefully you understand how the “newline_br()” method does its business, meaning that it’s time to continue extending the functionality of the sample “StringProcessor” class by means of another chainable method. In the next section I’m going to implement another method within the class, which will be responsible for replacing all of the new lines contained in a given string with paragraphs. To learn more about how this method will be coded, click on the link below and read the next segment.
blog comments powered by Disqus |
|
|
|
|
|
|
|