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

Defining a chainable factory method - 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 anticipated in the previous segment, it’s possible to define a factory method within the string processor class that returns instances of it, and that can also be chained to other methods.

To demonstrate how to create this chainable factory method, below I listed an enhanced version of the string processor class, which now includes the method in question. Here it is:

class StringProcessor

{

private $str = '';

private static $instance = NULL;

 

// factory method

// returns a singleton of string processor

// chainable

public static function factory($str)

{

if (self::$instance === NULL)

{

self::$instance = new StringProcessor($str);

}

return self::$instance;

}

 

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

}

}

If you pay attention to the current definition of the “StringProcessor” class, you’ll realize that it now includes a new method called “factory.” This method is not only completely chainable, but returns singletons of the class, in case you don’t want to deal with multiple instances.

Now that you’re aware of the functionality of this new factory method, here’s an example of how to use it in conjunction with others:

// process string by using factory method and chaining other multiple methods

echo StringProcessor::factory(' <strong>Hello '. "n" . 'this is a simple example of method chaining in PHP</strong> ')->

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>

*/

There you have it. As you can see, by directly calling the factory method of the string processor class, not only is it possible to chain multiple methods to filter a specified string, but there’s no need to deal explicitly with an instance of the class. The result of this process is a script whose code is extremely compact. Simple and effective, right?

Finally, with this last example I’m concluding  this tutorial on using chainable methods in PHP 5. As always, feel free to edit all of the code samples developed in it, so you can quicjkly grasp the underlying logic of this useful programming approach.

Final thoughts

In this second episode of the series, I explained how to extend the functionality of the string processor class by adding to it a few simple chainable methods, including one that creates instances of it as well.

In the next article, I’m going to complete the definition of this sample class by implementing some other chainable methods. These will come in handy for replacing specified characters with others in an inputted string, reversing its value and including HTML entities as well.

Don’t miss the upcoming part!



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