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

Working with static methods - 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

The best way to take advantage of the functionality given by the previous text helper class without dealing with its incidental instantiation is declaring its methods static.

To do this, Iím going to subtly modify the definition of the class in question, which now will look as follows:

class TextHelper

{

// constructor not implemented

public function __construct(){}

 

// convert new lines to '<br />' tags

public static function newline_br($str)

{

if (is_string($str) AND empty($str) === FALSE)

{

return nl2br($str);

}

}

 

// convert new lines to '<p>' tags in string

public static 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 static 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 static function uppercase_all($str)

{

if (is_string($str) AND empty($str) === FALSE)

{

return strtoupper($str);

}

}

 

// lowercase all characters in string

public static function lowercase_all($str)

{

if (is_string($str) AND empty($str) === FALSE)

{

return strtolower($str);

}

}

// uppercase first character in string

public static function uppercase_first($str)

{

if (is_string($str) AND empty($str) === FALSE)

{

return ucfirst(strtolower($str));

}

}

}

If you've frequently worked with static methods, then youíll quickly grasp the modified signature of the text helper class. Most of its source code remains practically the same, except for the static declaration of all of its methods (apart from the constructor).

Even though this modification seems to be insignificant at first sight, it introduces a great enhancement in the way that the class can be used. Why am I saying this? Well, as youíll realize, now itís feasible to call the classís method statically without having to create any objects from it. Simple and efficient.

But Iím not trying to sell you this simple helper class here. Instead, Iíd like to show you how to use it now that its methods have been defined as static.

Therefore, in the last section of this tutorial Iím going to create another example to demonstrate a proper usage of the text helper class, this time by calling its methods out of the object scope.

To see how this final example will be developed, please click on the link below and read the following segment.



 
 
>>> 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: