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

Using Method Call Overloading in PHP 4
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 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:
      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


    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


       · Over this second part of the series, you'll learn how to overload classes by using...
       · The __call method should return a boolean value letting php know whether or not you...
       · Thank you for your useful comments regarding the implementation of the __call()...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - 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
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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