As I anticipated in the previous segment, it’s possible to define a factory method within the string processor class that returns instances of it, and that can also be chained to other methods. To demonstrate how to create this chainable factory method, below I listed an enhanced version of the string processor class, which now includes the method in question. Here it is: 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; } } If you pay attention to the current definition of the “StringProcessor” class, you’ll realize that it now includes a new method called “factory.” This method is not only completely chainable, but returns singletons of the class, in case you don’t want to deal with multiple instances. Now that you’re aware of the functionality of this new factory method, here’s an example of how to use it in conjunction with others: // 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> */ There you have it. As you can see, by directly calling the factory method of the string processor class, not only is it possible to chain multiple methods to filter a specified string, but there’s no need to deal explicitly with an instance of the class. The result of this process is a script whose code is extremely compact. Simple and effective, right? Finally, with this last example I’m concluding this tutorial on using chainable methods in PHP 5. As always, feel free to edit all of the code samples developed in it, so you can quicjkly grasp the underlying logic of this useful programming approach. Final thoughts In this second episode of the series, I explained how to extend the functionality of the string processor class by adding to it a few simple chainable methods, including one that creates instances of it as well. In the next article, I’m going to complete the definition of this sample class by implementing some other chainable methods. These will come in handy for replacing specified characters with others in an inputted string, reversing its value and including HTML entities as well. Don’t miss the upcoming part!
blog comments powered by Disqus |
|
|
|
|
|
|
|