HomePHP Page 3 - Implementing Property Overloading in PHP 4
Overloading a PHP 4 class: using the native “overload()” function - 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.
Over the previous section, I created the sample “CookieSaver” class and went through the explanation of its “__set()” method, which comes in handy for modifying the elements of the $expTimes array. After defining the signature of this class, I’ll use the “overload()” function, in order to enable the overloading of its property accesses. The code listed below shows how to overload the “CookieSaver” class:
// overload 'CookieSaver' class overload('CookieSaver');
That’s it. Now, the sample class has been overloaded by the corresponding “overload()” function, which, as you saw, takes up the name of the class being overloaded as its unique parameter. Nevertheless, things get interesting right now since the “__set()” method can be triggered by overloading a property access. The example below demonstrates this process:
// instantiate 'CookieSaver' object $cookieSaver=&new CookieSaver(); // set cookie $cookieSaver->setCookie(); // call __set() method and modify $this->expTimes['exp1'] array element @$cookieSaver->exp1=60;
As you can see, the previous example clearly shows how to overload a property access, which automatically triggers the “__set()” method that you learned before. With reference to this, please, take a look at the following line:
With this statement, what I’m doing is accessing the array element $this->expTimes[‘exp1’] and assigning to it a value of 60 seconds. Of course, here the “magic” happens because the “__set()” method is called automatically by the PHP interpreter, resulting in the following output:
Setting new cookie...with an expiration of 60 seconds.
Wasn’t that cool? Once the previous class has been overloaded, it’s possible to run custom code by a property access, as I demonstrated by the above example. Right, I have to admit that the “@” error suppression character isn’t very elegant, but this one-liner allowed me to run code behind the scenes, all without an explicit call to the “__set()” method.
Provided that you understood the previous example, see the following sample code, where the remaining elements of the $expTimes array are modified in turn:
// call __set() method and modify $this->expTimes['exp2'] array element @$cookieSaver->exp2=120; // call __set() method and modify $this->expTimes['exp3'] array element @$cookieSaver->exp3=240;
After running the above script, the output I get on my browser is as follows:
Setting new cookie...with an expiration of 120 seconds. Setting new cookie...with an expiration of 240 seconds.
At this stage, you saw how a class can be overloaded in PHP 4, in order to trigger the respective “__set()” method and run the code wrapped by it. Now, let’s move forward and see how the corresponding “__get()” method can be triggered when a property access is appropriately overloaded.
The next few lines of the article explain how to achieve this.