One of the best things about using chainable methods is that it’s possible to add functionality to a determined class by progressively defining different links of this “chain,” which are tasked with performing well-differentiated tasks. That’s exactly the case with the method that I’m going to define in the next few lines. It will be charged with replacing all of the new lines contained in an inputted string with paragraphs. The signature of “StringProcessor” class, including this new chainable method called “newline_par()”, is shown below:
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; }
// 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; } } Undoubtedly, understanding how the “newline_par()” method works is a no-brainer process that can be tackled with minor effort. As I explained earlier, this chainable method will replace all of the new lines included in an input string with “<p>” tags, which makes it suitable for use as a typography helper. However, the best way to grasp the functionality of this method is by means of a concrete example, which has been coded below. Look at it, please:
$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()-> get_string(); /* displays the following <p>Hello </p><p>this is a simple example of method chaining in PHP</p> */ As shown above, the string processor class is now capable or replacing new lines with paragraphs, aside from a few other useful tasks performed by the existing chainable methods that you learned before. Of course, the actual beauty of this sample class rests in its ability to apply different filters to a given string by using a series of chainable methods that are simple to read and code. Period. It’s quite probable that you've noticed that each time an instance of this class is created, it is accomplished by directly calling its constructor. This is a typical way to create objects, as you know, but unfortunately the constructor can’t be chained to other methods. Nonetheless, it’s possible to overcome this small limitation by defining a factory method that not only returns instances of the class, but can be easily chained, too. So, as a fitting conclusion for this second article of the series, I’m going to explain how to create such a factory method. Now, go ahead and read the last section. We’re almost finished!
blog comments powered by Disqus |
|
|
|
|
|
|
|