PHP
  Home arrow PHP arrow Page 4 - Method Chaining: Adding More Methods to the Chain
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Method Chaining: Adding More Methods to the Chain
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-10-26


    Table of Contents:
  • Method Chaining: Adding More Methods to the Chain
  • Adding more methods to the string processor class
  • Adding another chainable method to the class
  • Defining a chainable factory method

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Method Chaining: Adding More Methods to the Chain - Defining a chainable factory method
    ( Page 4 of 4 )

    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
     

       

    PHP ARTICLES

    - Implementing the Data Mapper Design Pattern ...
    - Defining an Abstract Class with Restrictive ...
    - The Reflection API: Working with Reflected M...
    - Using Restrictive Constructors in PHP 5
    - Getting Information on a Reflected Class wit...
    - Introducing the Reflection API in PHP 5
    - Swift Mailer's Batchsend Method and Other Fe...
    - Embedding Attachments into Email Messages wi...
    - Dynamically Attaching Files with Swift Mailer
    - Using Different Paths for Attachments with S...
    - Handling Attachments and Sending HTML Email ...
    - Sending Blind Carbon Copies with Swift Mailer
    - Using Swift Mailer's Cc MIME Header
    - Using Sendmail and Mail() with Swift Mailer
    - Using Different SMTP Transports with Swift M...


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code



    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 10 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek