Home arrow PHP arrow Page 2 - Method Chaining: Adding More Methods to the Chain

Adding more methods to the string processor class - PHP

In this second part of a 12-part series on method chaining, I explain how to extend the functionality of the string processor class by adding to it a few simple chainable methods. I conclude this part by defining a factory method within the string processor class.

TABLE OF CONTENTS:
  1. Method Chaining: Adding More Methods to the Chain
  2. Adding more methods to the string processor class
  3. Adding another chainable method to the class
  4. Defining a chainable factory method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
October 26, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I said in the introduction, I’d like to extend the existing functionality of the string processor class coded in the previous article by adding some additional methods to it. This will show you how the method chaining approach can be used to accomplish this task in a simple manner.

So, with that idea in mind, below I listed an enhanced version of the string processor class. It includes a brand new chainable method called “newline_br()”, which is a wrapper for the native “nl2br()” native PHP function. Here it is:

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;

}

 

// convert new lines to <br /> tags

// chainable

public function newline_br()

{

$this->str = nl2br($this->str);

return $this;

}

// get input string

public function get_string()

{

return $this->str;

}

}

As I explained, now the above “StringProcessor” class implements a new chainable method named “newline_br()”, which is a simple proxy for the “nl2br()” PHP function. Of course, the most relevant aspect of this method is the fact that it’s completely chainable, so it can be used in conjunction with others to replace all of the new lines included in a given string by “<br /”> tags.

To demonstrate the functionality of this new chainable method, below I listed another code sample that shows how to work with it. Have a look at the  example:

$strproc = new StringProcessor(' <p>Hello '. "n" . 'this is a simple example of method chaining in PHP</p> ');

// process string by chaining multiple methods

echo $strproc->trim_space()->

remove_tags()->

newline_br()->

get_string();

/*

displays the following

Hello <br />

this is a simple example of method chaining in PHP

*/

That was not rocket science, right? In the previous example the “newline_br()” method has been chained to some existing ones to replace all of the new line characters included in a sample string by “<br />” tags. The output generated by this particular example demonstrates how compact and tight the code looks when all of these methods are chained in one single step.

So far, so good. At this stage hopefully you understand how the “newline_br()” method does its business, meaning that it’s time to continue extending the functionality of the sample “StringProcessor” class by means of another chainable method.

In the next section I’m going to implement another method within the class, which will be responsible for replacing all of the new lines contained in a given string with paragraphs.

To learn more about how this method will be coded, click on the link below and read the next 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: