PHP
  Home arrow PHP arrow Page 3 - Using Method Call 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

Using Method Call Overloading in PHP 4
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2006-07-18


    Table of Contents:
  • Using Method Call Overloading in PHP 4
  • Going backwards: a quick look at a previous example
  • Overloading multiple property accesses: combining the “__set()” and “__get()” methods in a single class
  • Triggering the “__call()” method in the background: overloading a method call

  • 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


    Using Method Call Overloading in PHP 4 - Overloading multiple property accesses: combining the “__set()” and “__get()” methods in a single class
    ( Page 3 of 4 )

    Combining the “__set()” and “__get()” methods into one single class is really a no-brainer process. Once the structure of the class has been defined, it’s just a matter of adding a concrete implementation for each of the methods. Using the previous “CookieSaver” class, here is how the two respective methods can be combined:

    class CookieSaver{
        var $cookieName;
        var $value;
        var $expTimes=array('exp1'=>900,'exp2'=>1800,'exp3'=>3600);
        function CookieSaver($cookieName='defaultCookie',$value='defaultValue'){
            if(!is_string($cookieName)){
                trigger_error('Invalid cookie name',E_USER_ERROR);
            }
                $this->cookieName=$cookieName;
                $this->value=$value;
        }
        // set cookie
        function setCookie(){
            setcookie($this->cookieName,$this->value);
        }
        // get cookie
        function getCookie(){
            if(!$cookie=$_COOKIE[$this->cookieName]){
                trigger_error('Error retrieving
    cookie',E_USER_ERROR);
            }
            return $cookie;
        }
        // set value of property via __set() method
        function __set($property,$value){
            $this->expTimes[$property]=$value;
            $expTime=$this->expTimes[$property];
            setcookie('newCookie',urlencode('This cookie has been set
    via the __set() method'),time()+$expTime);
            echo 'Setting new cookie...with expiration of '.$expTime.' seconds.';
            return;
        }
        // get value of property via __get() method
        function __get($property){
            $expTime=$this->expTimes[$property];
            setcookie('newCookie',urlencode('This cookie has been set
    via the __get() method'),time()+$expTime);
            echo 'Retrieving new cookie...with an expiration of
    '.$expTime.' seconds.';
            return;
        }
    }

    As you can see, the above class now includes both the “__set()” and “__get()” methods that you saw previously. This comes in very handy for demonstrating how they can be triggered together when the class is properly overloaded by the “overload()” function.

    With reference to this, the following code snippet shows how to overload the class, and how the corresponding “__get()” and “__set()” method are automatically called behind the scenes when the same property access is overloaded in turn:

    // overload 'CookieSaver' class ( implements __set() and __get()
    methods)
    overload('CookieSaver');
    // instantiate 'CookieSaver' object
    $cookieSaver=&new CookieSaver();
    // call __set() method and modify $this->expTimes['exp1'] array
    element
    @$cookieSaver->exp1=60;
    // call __get() method and return $this->expTimes['exp1'] array
    element
    echo @$cookieSaver->exp1;

    In the previous example, when the first property access is overloaded, it gives as output the following message:

    Setting new cookie...with an expiration of 60 seconds.

    Obviously, this means that the “__set()” method has been triggered by the PHP parser and a new cookie has been set. In a similar fashion, when the second property access is overloaded, the script displays the following message:

    Retrieving new cookie...with an expiration of 900 seconds.

    This time, after overloading the second property access, the “_get()” method has been called, and as a result, the cookie has been retrieved by this method. Also, it should be noted that both property accesses must not be overloaded at the same time. An error will be triggered if you do this, because the two “__set()” and “__get()” methods first set a cookie and then display a simple message. As you know, you cannot output anything before handling cookies (at least not directly) when using the HTTP protocol.

    Right, at this point you’ve seen how the two “__set()” and “__get()” methods were combined within the same sample class, to be triggered by the PHP interpreter when a couple of property accesses are overloaded. Assuming that you already grasped the core concepts of how to overload a property access, the last example that I’m going to show you in this article is aimed at demonstrating how to overload a method call. It will result in the triggering of the respective “__call()” method.

    To learn how this will be achieved, please jump into the next section and keep on reading.



     
     
    >>> 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek