PHP
  Home arrow PHP arrow Page 3 - 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? 
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 - Method overloading in PHP 5 with the call magic function
    ( Page 3 of 4 )

     

    Quite probably, one of the most helpful methods that fall under the category of magic functions is the popular "__call()." If you still haven't had the opportunity to use it with your programs, the "__call()" function is invoked behind the scenes by the PHP parser when it finds a reference to a method of a class that haven't been explicitly defined.

    So, on one hand there's the pair composed by the "__set()" and "__get()" functions, which allow you to overload properties of a class, while on the other hand, "__call()" permits you to overload methods extremely easily.

    Of course, this way of overloading properties and methods is only a proprietary feature of PHP 5 that differs significantly from other mature languages, such as Java and C++, but it can be really helpful when used in a creative manner.

    Now that you have a more clear idea of when the "__call()" function is invoked by the PHP engine, let me show you two concrete examples, so you can spot the differences between them. In the first case, the "User" class that you saw before will be used without implementing the "__call()" method, while in the second example, the method will be implemented explicitly.

    So, here's the "User" class without using method overloading:

    class User

    {

    // constructor (not implemented)

    public function _construct(){}

     

    // set undeclared property in a restrictive way

    public function __set($property, $value)

    {

    if (in_array($property, array('fname', 'lname', 'email')) === TRUE)

    {

    $this->$property = $value;

    }

    }

     

    // get declared property

    public function __get($property)

    {

    if (isset($this->$property))

    {

    return $this->$property;

    }

    }

    }

     

     

    // example of usage of 'User' class with no 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');

     

    /*

    triggers a fatal error:

    Fatal error: Call to undefined method User::fetch() in path/to/file

    */

    So far, nothing unexpected is happening here, since the above script attempts to retrieve the values assigned to the properties of a fictional user via a non-existent method called "fetch()." Naturally, this makes the PHP parser raise a fatal error.

    However, this issue can be addressed quickly if the same class gives a concrete implementation to the "__call()" method. Want to see how this can be done? Well, take a look at the new definition of this class, which is as follows:

    class User

    {

    // constructor (not implemented)

    public function _construct(){}

     

    // set undeclared property in a restrictive way

    public function __set($property, $value)

    {

    if (in_array($property, array('fname', 'lname', 'email')) === TRUE)

    {

    $this->$property = $value;

    }

    }

     

    // get declared property

    public function __get($property)

    {

    if (isset($this->$property))

    {

    return $this->$property;

    }

    }

     

    // single point to fetch user data

    public function __call($method, $args)

    {

    if ($method === 'fetch' AND empty($args) === FALSE)

    {

    return $this->$args[0];

    }

    }

    }

    Definitely, things are getting much more interesting now. As you can see, the enhanced version of the "User" class includes the "__call()" method, which checks to see if the non-existent method being called is named "fetch()" or not. If this happens to be true, it'll return the first argument that was passed in to it.

    Implementing the "__call()" function in that way will allow us to use the non-declared "fetch()" method to retrieve the values assigned to the properties of the "User" class in a really simple fashion.

    But guess what? The full details of this process will be discussed in the last section of this tutorial. Therefore, to get there, click on the link below and keep reading.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT