Home arrow PHP arrow Page 4 - Building Helpers in PHP 5

The text helper class in action - PHP

Tired of repetitive coding for tasks such as generating web site breadcrumbs? PHP 5 provides a number of interesting ways to reuse your code so you can let the boring stuff take care of itself. This eight-part series will show you how to create helpers to take care of repetitive tasks and free up your precious coding time for the real challenges.

TABLE OF CONTENTS:
  1. Building Helpers in PHP 5
  2. Building a helper in PHP 5
  3. Extending the functionality of the text helper class
  4. The text helper class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
July 20, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I said in the previous section, it's necessary to create an example that shows how to use the "TextHelper" class that you learned before to apply the filters to a given string. Therefore, please look at the following code sample, which  illustrates how to accomplish this process in a simple manner. Here it is:

// 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!

*/

Definitely, the "TextHelper" class isn't going to change the way you develop your PHP applications, but it'll hopefully show you how to use their methods for applying different formatting filters to a supplied literal.

Of course, as I said before, the current functionality of the class can be largely improved, but this task will be left as homework for you, so you can entertain yourself for many hours.

Final thoughts

That's all for now. In this first article of the series, I offered a friendly introduction to building helper classes with PHP 5. As you saw earlier, this process is extremely straightforward; it's reduced to applying the basis of the object-oriented approach and nothing else.

In the upcoming part, I'm going to explain how to take advantage of the functionality offered by the previous text helper class without creating an instance of it, simply by statically calling their methods.

Don't miss the next tutorial!



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