PHP
  Home arrow PHP arrow Page 3 - Implementing Property Overloading in P...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Implementing Property Overloading in PHP 4
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2006-07-11

    Table of Contents:
  • Implementing Property Overloading in PHP 4
  • Overloading a property access: using the “__set()” method
  • Overloading a PHP 4 class: using the native “overload()” function
  • Going deeper into property access overloading: using the “__get()” method

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Implementing Property Overloading in PHP 4 - Overloading a PHP 4 class: using the native “overload()” function


    (Page 3 of 4 )

    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


       · One of the most uncovered aspects of PHP is precisely the overloading of methods and...
       · The __get, __set and __call methods must return a boolean value that tell php...
       · Thank you for commenting on my PHP article. Also, I thank you for the useful...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT