Home arrow PHP arrow Page 2 - PHP 5 Helpers: Calling Methods Out of Object Scope

Review: building a text helper class - PHP

Theoretically defining the role of helper classes in PHP 5 (and other popular server-side scripting languages) is a pretty approachable process. As their name suggests, helper classes provide developers with a set of logically-grouped methods that allow them to easily perform certain repetitive tasks that are common to different web applications. This is the second part of an eight-part article series that shows you how to build a variety of useful helper classes and expand on their functionality.

TABLE OF CONTENTS:
  1. PHP 5 Helpers: Calling Methods Out of Object Scope
  2. Review: building a text helper class
  3. Working with static methods
  4. The new text helper class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
July 27, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: