PHP
  Home arrow PHP arrow Method Chaining in PHP 5
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 in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2009-10-21


    Table of Contents:
  • Method Chaining in PHP 5
  • Building a simple string processor
  • Coding another chainable method
  • Removing unwanted tags from input strings

  • 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 in PHP 5
    ( Page 1 of 4 )

    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.

    Over the course of the last few years PHP has reinvented itself; that's why it's the mature language that we see nowadays. The most significant change introduced by the PHP development team is the release of PHP 5, which incorporates a robust object model, a good exception handling mechanism, type hinting, the filter library, and many other features that you've probably used hundreds of times before.

    While it's true that PHP 5 does allow you to implement the object-oriented paradigm in a much more efficient and elegant manner, other fundamental concepts inherent to software programming can be applied to both PHP 4 and PHP 5 similarly.

    To cite a few examples, common design patters like Factory, Singleton and Model-View-Controller (add your own to the list) can be implemented pretty easily in both versions of PHP, since they have to do more with proven programming methodologies than with the language's intrinsic features.

    One great technique that can be used when developing PHP applications is popularly known as method chaining. For those just learning PHP, it may sound intimidating, particularly if they're not familiar with classes and objects and their methods, obviously. But the truth is that method chaining is an approach that can be mastered with minor efforts.

    But, what is method chaining? Well, let me introduce you to the subject gently with a simple example that you'll quickly grasp. Say that you're building a MySQL abstraction class that implements some methods that will be used for performing queries against a selected database table.

    In a typical situation, there will be only one method that will run SELECT statements, which should be used in conjunction with common query modifiers (such as WHERE, LIKE, LIMIT and ORDER BY SQL clauses) to perform conditional queries. However, it's possible to implement multiple methods instead, where each one be will be responsible only for executing raw SELECTS, WHERE and ORDER BY clauses, and so forth.

    With these methods coded in that way, it's feasible to call them chained according to specific needs, and get a result set which can be processed in other clever ways. This chaining process can be easily accomplished by making the methods  return an instance of the MySQL abstraction class to calling code. Simple and effective.

    High-quality frameworks like Kohana and CodeIgniter use method chaining within their database classes. In fact, Kohana will let you use method chaining with many other classes as well. Method chaining makes the code more compact and robust.

    So, assuming that you're interested in learning how to use method chaining within your own PHP applications, in this series of articles I'm going to create some examples that will show you how useful this approach can be. I'm going to start by developing a trivial string processor class to demonstrate the implementation of this approach. When I complete this application, I'll build more complex programs. Now, let's start discovering the real power behind method chaining in PHP 5!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





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