PHP
  Home arrow PHP arrow Page 4 - The Call Magic Function 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

The Call Magic Function in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2009-06-08


    Table of Contents:
  • The Call Magic Function in PHP 5
  • Review: the isset and unset magic functions
  • Method overloading in PHP 5 with the call magic function
  • The call magic method in action

  • 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


    The Call Magic Function in PHP 5 - The call magic method in action
    ( Page 4 of 4 )

     

    In the prior segment, I explained how to implement the "__call()" functions to intercept all of the calls made to non-existent methods of the sample "User" class. In this particular case, the function will accept only requests for a "fetch()" method, which will be used for retrieving the values assigned to the class's properties. For you to understand how to accomplish this retrieval process, you'll find a short example below that shows how to use the undeclared "fetch()" method without triggering a fatal error.

    Here it is:

    // example on utilizing 'User' class with method overloading

    $user = new User();

    $user->fname = 'Alejandro';

    $user->lname = 'Gervasio';

    $user->email = 'alejandro@mydomain.com';

     

    // display user data

    echo 'First Name : ' . $user->fetch('fname') . ' Last Name : ' . $user->fetch('lname') . ' Email : ' . $user->fetch('email');

     

    /*

    display the following

    First Name : Alejandro Last Name : Gervasio Email : alejandro@mydomain.com

    */

    There you have it. Now, via the handy "__call()" magic function it's possible to overload the "fetch()" method and use it for retrieving the values given to the properties of the "User" class. Of course, you can implement the "__call()" function in the way that best suits your needs; that process will logically depend on the context in which this function will be used.

    Finally, feel free to edit all of the code samples shown in this tutorial, so you can acquire a more solid foundation in working with the set of magic functions included with PHP 5.

    Final thoughts

    It's hard to believe, but we've come to the end of this third part of the series. Nevertheless, the whole experience has been instructive, since you learned how to use the "__call()" magic function to implement method overloading in PHP 5.

    Definitely, I don't mean to dismiss the importance of other magic functions, but possibly the trio composed by the "__set()", "__get()" and "__call()" methods is one of the most powerful ones when it comes to simplifying the tasks performed by a particular class. Naturally, this simplification comes at a cost, very frequently in terms of poorer readability and code maintenance, so make sure not to abuse them when developing your own web applications.

    Moving forward, in the next installment of the series I'm going to continue exploring more magic functions bundled with PHP 5. That particular tutorial will be focused on discussing the "__sleep()" and "__wakeup()" functions, which are invoked automatically by the PHP engine when serializing/unserializing objects.

    Don't miss the upcoming part!



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek