Home arrow PHP arrow Page 4 - Implementing Property Overloading in PHP 4

Going deeper into property access overloading: using the “__get()” method - PHP

The object-oriented paradigm includes the ability to overload classes; not surprisingly, this is possible in PHP. In this first part of a series, you will learn how to implement class overloading in PHP 4, specifically using the “overload()” function, which comes in handy for triggering “__set()” and “__get()” methods when a property access is overloaded.

TABLE OF CONTENTS:
  1. Implementing Property Overloading in PHP 4
  2. Overloading a property access: using the “__set()” method
  3. Overloading a PHP 4 class: using the native “overload()” function
  4. Going deeper into property access overloading: using the “__get()” method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
July 11, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Remember that I said it was also possible to trigger a “__get()” method when a property access is overloaded? Fine, because that is what I’d like to show you in this section of the tutorial.

In order to demonstrate how this process is performed, I’ll redefine the previous “CookieSaver” class, this time replacing its “__set()” method by a “__get()” method. This really sounds simple, so here’s the source code of the modified class:

// define 'CookieSaver' class and implement __get() method
class CookieSaver{
    var $cookieName;
    var $value;
    var $expTimes=array('exp1'=>900,'exp2'=>1800,'exp3'=>3600);
    function CookieSaver
($cookieName='defaultCookie',$value='defaultValue'){
        if(!is_string($cookieName)){
            trigger_error('Invalid cookie name',E_USER_ERROR);
        }
        $this->cookieName=$cookieName;
        $this->value=$value;
    }
    // set cookie
    function setCookie(){
        setcookie($this->cookieName,$this->value);
    }
    // get cookie
    function getCookie(){
        if(!$cookie=$_COOKIE[$this->cookieName]){
            trigger_error('Error retrieving
cookie',E_USER_ERROR);
        }
        return $cookie;
    }
    // get value of property via __get() method
    function __get($property){
        $expTime=$this->expTimes[$property];
        setcookie('newCookie',urlencode('This cookie has been set
via the __get() method'),time()+$expTime);
        echo 'Retrieving new cookie...with an expiration of
'.$expTime.' seconds.';
        return;
    }
}

As I illustrated above, now the “CookieSaver” class specifically implements a “__get()” method, instead of exposing the previous “__set()” method that you learned before. Also, notice that the definitions of the remaining methods are the same, which means obviously that the class sets and retrieves a simple cookie via its “setCookie()” and “getCookie()” methods respectively.

As you can see, now the “__get()” method has been defined in such a way that it retrieves a specific element of the $expTimes array and assigns this value to the expiry of the cookie in question. From this example, you’ll surely realize that the generic signature of a “__get()” method is the following:

__get($property){
    // method definition goes here
}

By this point, after the prior __”get()” method has been explicitly implemented, it’s time to see how it can be triggered, overloading a specific property access. The code snippet below shows the process that calls the “__get()” method, when a property access is performed:

// overload 'CookieSaver' class
overload('CookieSaver');
// instantiate 'CookieSaver' object
$cookieSaver=&new CookieSaver();
// set cookie
$cookieSaver->setCookie();
// call __get() method and retrieve $this->expTimes['exp1'] array element
echo @$cookieSaver->exp1;

In the above example, the “CookieSaver” class is first overloaded by using the “overload()” function, and then the “__get()” method is triggered by the property accessing line listed below:

echo @$cookieSaver->exp1;

This statement executes the code defined inside of the “__get()” method, which results in the following output:

Retrieving new cookie...with an expiration of 900 seconds.

In a similar way, it’s perfectly feasible to access the remaining elements of the $expTimes array, like this:

// call __get() method and retrieve $this->expTimes['exp2'] array
element
echo @$cookieSaver->exp2;
// call __get() method and retrieve $this->expTimes['exp3'] array
element
echo $cookieSaver->exp3;

In both cases, the “__get()” method is automatically called and the output produced by the previous examples is the following:

Retrieving new cookie...with an expiration of 1800 seconds.
Retrieving new cookie...with an expiration of 3600 seconds.
 

As you’ll realize, the PHP parser automatically triggers the execution of the “__get()” method, if there’s a line of code that overloads a property access, just like the examples shown a few lines above.

At this stage, I hope that you already understand the logic that surrounds the overloading of classes in PHP 4. Even when the examples are just that, they show in a simple way how to execute custom code without having to explicitly call both “__set()” and “__get()” methods. Certainly, there’s plenty of room to experiment with this concept, and you’ll probably find more useful applications when overloading your PHP classes.

To wrap up

In this first part of the series, I explored the implementation of class overloading in PHP 4, specifically using the “overload()” function, which comes in handy for triggering “__set()” and  “__get()” methods, when a property access is overloaded.

However, we’re just beginning the journey. Over the next tutorial, I’ll explain how to combine the two “__set()” and “__get()” methods inside the same class, as well as how to trigger the “__call()” function via method call overloading. You don’t have any excuses to miss it! 



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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