PHP
  Home arrow PHP arrow Page 3 - Implementing Property Overloading in PHP 4
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Implementing Property Overloading in PHP 4
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek