Home arrow PHP arrow Page 4 - Using Method Call Overloading in PHP 4

Triggering the “__call()” method in the background: overloading a method call - PHP

This is part two of the series “Overloading classes in PHP.” In three tutorials, this series teaches how to overload your classes in PHP 4 by using the “overload()” PHP built-in function, in conjunction with the implementation of the “__set()”, “__get()” and “__call()” methods, and explores the native support of object overloading in PHP 5.

  1. Using Method Call Overloading in PHP 4
  2. Going backwards: a quick look at a previous example
  3. Overloading multiple property accesses: combining the “__set()” and “__get()” methods in a single class
  4. Triggering the “__call()” method in the background: overloading a method call
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
July 18, 2006

print this article



As I mentioned before, the last example that I’ll show you illustrates how a “__call()” method can be triggered when a method call is overloaded. To clarify this concept, let me tell you that if a specific class has been overloaded by the “overload()” function, then it’s possible to trigger a “__call()” method automatically by using a method call, if the class provides a concrete implementation of it.

Does this sound confusing? Well, in order to simplify things, first I’ll redefine the previous “CookieSaver” class, so it will provide a concrete definition of a “__call()” method. This is how the class now looks:

// define 'CookieSaver' class and implement __call() method
class CookieSaver{
    var $cookieName;
    var $value;
    var $expTimes=array('exp1'=>900,'exp2'=>1800,'exp3'=>3600);
    function CookieSaver
            trigger_error('Invalid cookie name',E_USER_ERROR);
    // set cookie
    function setCookie(){
    // get cookie
    function getCookie(){
            trigger_error('Error retrieving
        return $cookie;
    // set cookie via __call () method
    function __call($method,$arguments){
        setcookie('newCookie',urlencode('This cookie has been set
via the __call() method'),time()+$expTime);
        echo 'Calling '.$method.'() method, which sets a new
cookie with an expiration of '.$expTime.' seconds.';

As shown above, the “CookieSaver” class looks nearly identical to the previous examples, except that now the class provides a concrete implementation of the “__call()” method. As you can see, the generic structure of this method is the following:

function __call($method,$arguments){
    // method definition goes here

The skeleton of this method should give you a clearer idea, with reference to its input parameters. Yep, you guessed right; when this method is called, it will take up the name of the initial method that was invoked along with an array containing its arguments. Here’s an example that shows the complete process for overloading a method call, which results in the triggering of the previous “_call()” method. Please study the code listed below:

// overload 'CookieSaver' class and implement the __call() method
// instantiate 'CookieSaver' object
$cookieSaver=&new CookieSaver();
// set cookie via __call() method

Things are getting exciting now! If you analyze the above script, you’ll notice the following line:


What I’m doing here basically is overloading a method call, in this case “setExpTime()”, in order to trigger the corresponding “__call()” method. As you noticed, I’m using a nonexistent method to do this, so I coded the “@” error suppressor before the expression.

Of course, I have to admit the code isn’t elegant at all, but it does trigger the “__call()” method, as shown below:

Calling setexptime() method, which sets a new cookie with an
expiration of 900 seconds.

As you saw, the example above shows a crude implementation of overloading a method call, which results in the pertinent “__call()” method being triggered by the PHP parser. Again, I’d like to emphasize that the example focuses on demonstrating the functionality of method call overloading, to the detriment of the code's elegance.

Wrapping up

Over this second part of the series I showed you how to combine the “__set()” and “__get()” methods inside the same class, in order to trigger them when a pair of property accesses are overloaded. Also, you learned how to overload a method call, which triggers the corresponding “__call()” method. Since class overloading isn’t natively supported in PHP 4, I used some code workarounds to keep things working properly.

In the last tutorial, I’ll teach you how to implement object overloading in PHP 5. See you in the next part!

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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