Home arrow PHP arrow 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.

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

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:

@$cookieSaver->exp1=60;

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.



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