If you haven’t had the chance to read the introductory part of this series, where I went through the development of a basic text helper class with PHP 5, don't worry; below I included the full source code of this class, so you can not only take a quick look at it, but quickly grasp how it works. So, in summary, here’s how the class was initially defined, along with some examples regarding its proper usage: class TextHelper { // constructor (not implemented) public function __construct(){}
// convert new lines to '<br />' tags public function newline_br($str) { if (is_string($str) AND empty($str) === FALSE) { return nl2br($str); } }
// convert new lines to '<p>' tags in string public function newline_par($str, $id = '', $class = '') { if (is_string($str) AND empty($str) === FALSE) { if ($id != '') { $id = ' id="' . $id . '"'; } if ($class != '') { $class = ' class="' . $class . '"'; } return '<p' . $id . $class . '>' . str_replace("n", '</p><p>', $str) . '</p>'; } }
// convert new lines to 'div' tags in string (id and class attributes can also be specified) public function newline_div($str, $id = '', $class = '') { if (is_string($str) AND empty($str) === FALSE) { if ($id != '') { $id = ' id="' . $id . '"'; } if ($class != '') { $class = ' class="' . $class . '"'; } return '<div' . $id . $class . '>' . str_replace("n", '<div></div>', $str) . '</div>'; } }
// uppercase all characters in string public function uppercase_all($str) { if (is_string($str) AND empty($str) === FALSE) { return strtoupper($str); } }
// lowercase all characters in string public function lowercase_all($str) { if (is_string($str) AND empty($str) === FALSE) { return strtolower($str); } } // uppercase first character in string public function uppercase_first($str) { if (is_string($str) AND empty($str) === FALSE) { return ucfirst(strtolower($str)); } } }
// create an instance of TextHelper class $txthelper = new TextHelper(); // convert new lines to '<br>' tags echo $txthelper->newline_br('The shinning ' . "n" . 'is a scary book of Stephen King!'); /*displays the following The shinning <br /> is a scary book of Stephen King! */
// convert new lines to '<p>' tags echo $txthelper->newline_par('The shinning ' . "n" . 'is a scary book of Stephen King!', 'pid', 'pclass'); /* displays the following <p id="pid" class="pclass">The shinning </p><p>is a scary book of Stephen King!</p> */
// convert new lines to '<div>' tags echo $txthelper->newline_div('the shinning ' . "n" . 'is a scary book of Stephen King!', 'divid', 'divclass'); /* displays the following <div id="divid" class="divclass">the shinning <div></div>is a scary book of Stephen King!</div> */
// uppercase sample string echo $txthelper->uppercase_all('The shinning is a scary book of Stephen King!'); /* displays the following THE SHINNING IS A SCARY BOOK OF STEPHEN KING! */
// lowercase sample string echo $txthelper->lowercase_all('The shinning is a scary book of Stephen King!'); /* displays the following the shinning is a scary book of stephen king! */
// uppercase first character in sample string echo $txthelper->uppercase_first('the shinning is a scary book of Stephen King!'); /* displays the following The shinning is a scary book of stephen king! */ As you can see above, I listed the complete signature of the “TextHelper,” along with a few easy-to-follow examples of how to use its methods for applying several filters to a sample string. Aside from the ease of the examples in question, it's valid to stress something here that becomes crucial to correctly using a helper class. As you may have noticed, in all the cases, the methods of the class have been called in the object context. Obviously, this implies that an instance of the class has been previously created. As I expressed in the introduction, however, this circumstance can be completely avoided by calling the methods statically. Of course, the PHP engine will allow you to do this even when the methods haven’t been declared explicitly static, but this certainly may lead to an eventual misuse of the helper class. To solve this issue, it would be much more effective to add a explicit “static” declaration to all of the class’s methods. This will prevent eventual calls inside an object context. So, in the section to come I’m going to modify the initial definition of the “TextHelper” class by converting its regular public methods into static ones. Logically, if you want to learn the full details of this conversion process, you’ll have to click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|