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

Going backwards: a quick look at a previous example - 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



Before I explain how to combine the two “__set()” and “__get()” methods within the same overloaded class, you should take a brief look at the sample “CookieSaver” class that I wrote in the first article, which provides a concrete implementation of the “__set()” method. Here is the signature of this class, according to its original definition:

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 value of property via __set() method
    function __set($property,$value){
        setcookie('newCookie',urlencode('This cookie has been set
via the __set() method'),time()+$expTime);
        echo 'Setting new cookie...with an expiration of '.$expTime.' seconds.';

Assuming that the above class is now fresh in your mind, below I listed a simple script that triggers the “_set()” method when a property access is overloaded. Take a look please:

// instantiate 'CookieSaver' object
$cookieSaver=&new CookieSaver();
// set cookie
// call __set() method and modify $this->expTimes['exp1'] array

In this case, the previous code sample demonstrates a crude implementation of how to overload a property access, in order to automatically run the code defined inside the __”set()” method. After running the previous script, its output is the following:

Setting new cookie...with an expiration of 3600 seconds.

Now that you remember how to call a “__set()” method, and also a “__get()” method via the corresponding overloading of a property access, let’s see how both methods can be combined inside the same “CookieSaver” class. To learn how this will be done, please read the following section of the article.

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