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

Adding another chainable method to the 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
 

One of the best things about using chainable methods is that it’s possible to add functionality to a determined class by progressively defining different links of this “chain,” which are tasked with performing well-differentiated tasks. That’s exactly the case with the method that I’m going to define in the next few lines. It will be charged with replacing all of the new lines contained in an inputted string with paragraphs. 

The signature of “StringProcessor” class, including this new chainable method called “newline_par()”, is shown below:

 

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;

}

 

// convert new lines to paragraphs

// chainable

public function newline_par()

{

$this->str = '<p>' . str_replace("n", '</p><p>', $this->str) . '</p>';

return $this;

}

 

// get input string

public function get_string()

{

return $this->str;

}

}

Undoubtedly, understanding how the “newline_par()” method works is a no-brainer process that can be tackled with minor effort. As I explained earlier, this chainable method will replace all of the new lines included in an input string with “<p>” tags, which makes it suitable for use as a typography helper.

However, the best way to grasp the functionality of this method is by means of a concrete example, which has been coded below. Look at it, please:

 

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

// process string by chaining multiple methods

echo $strproc->trim_space()->

remove_tags()->

newline_par()->

get_string();

/*

displays the following

<p>Hello </p><p>this is a simple example of method chaining in PHP</p>

*/ 

As shown above, the string processor class is now capable or replacing new lines with paragraphs, aside from a few other useful tasks performed by the existing chainable methods that you learned before.

Of course, the actual beauty of this sample class rests in its ability to apply different filters to a given string by using a series of chainable methods that are simple to read and code. Period.

It’s quite probable that you've noticed that each time an instance of this class is created, it is accomplished by directly calling its constructor. This is a typical way to create objects, as you know, but unfortunately the constructor can’t be chained to other methods.

Nonetheless, it’s possible to overcome this small limitation by defining a factory method that not only returns instances of the class, but can be easily chained, too. So, as a fitting conclusion for this second article of the series, I’m going to explain how to create such a factory method.

Now, go ahead and read the last section. We’re almost finished!



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