Home arrow PHP arrow Page 4 - Method Chaining in PHP 5

Removing unwanted tags from input strings - PHP

Method chaining is a useful technique that helps you to write more compact and powerful code. In this first part of a series that introduces you to this programming methodology, you'll learn how to define and use chainable methods in PHP 5. The process is quite simple to grasp, even if you have only an intermediate background in using the object-oriented paradigm.

TABLE OF CONTENTS:
  1. Method Chaining in PHP 5
  2. Building a simple string processor
  3. Coding another chainable method
  4. Removing unwanted tags from input strings
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
October 21, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



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