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 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){ 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 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 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. 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!
blog comments powered by Disqus |
|
|
|
|
|
|
|