As I stated in the previous section, I'd like to finish this first chapter of the series by showing you how to provide the "StringProcessor" class with yet another chainable method. As you'll see in a moment, it will be tasked with removing unwanted PHP and HTML tags from an input string. The current definition of the class after implementing this whole new method looks like this: 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; } // get input string public function get_string() { return $this->str; } } Definitely, the implementation of the above "remove_tags()" method is very simple to follow. It's merely a proxy method for the native "strip_tags()" native PHP function. But naturally, the most interesting facet of it rests on its ability to be chained to other methods, as demonstrated by the following code sample: $strproc = new StringProcessor(' <p>Hello, this is a simple example of method chaining in PHP</p> '); // process string by chaining 3 methods echo $strproc->trim_space()-> remove_tags()-> get_string(); /* displays the following hello, this is a simple example of method chaining in php */ Now, the string processor class is capable of performing quite a few useful tasks on a given string, such as trimming white spaces, lowercasing and upper casing its characters, and finally removing tags by means of some chainable methods, whose implementation is really easy to grasp. Of course, the class itself is not suited for production use, since it's only a starting example that shows how to chain methods in PHP 5. However, and this would be homework for you, I suggest you try adding more chainable methods to the class, to give you more practice in this useful programming methodology. Final thoughts In this first installment of the series, I provided you with a hopefully gentle introduction to defining and using chainable methods in PHP 5. As you saw for yourself before, the whole process is quite simple to grasp, even if you have only an intermediate background in using the object-oriented paradigm. Also, as you may have noticed, the string processor class built previously defines a static property called "$intance," which has remained unused so far. Well, the reason for this is that I plan to create a factory method for the class that only returns singletons of it, and that also will be chainable. This topic will be covered in depth in the next part of the series. Thus, you don't have any excuses to miss it!
blog comments powered by Disqus |
|
|
|
|
|
|
|