PHP
  Home arrow PHP arrow Page 4 - 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 - Triggering the “__call()” method in the background: overloading a method call


    (Page 4 of 4 )

    As I mentioned before, the last example that I’ll show you illustrates how a “__call()” method can be triggered when a method call is overloaded. To clarify this concept, let me tell you that if a specific class has been overloaded by the “overload()” function, then it’s possible to trigger a “__call()” method automatically by using a method call, if the class provides a concrete implementation of it.

    Does this sound confusing? Well, in order to simplify things, first I’ll redefine the previous “CookieSaver” class, so it will provide a concrete definition of a “__call()” method. This is how the class now looks:

    // define 'CookieSaver' class and implement __call() method
    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 cookie via __call () method
        function __call($method,$arguments){
            $expTime=$this->expTimes[$arguments[0]];
            setcookie('newCookie',urlencode('This cookie has been set
    via the __call() method'),time()+$expTime);
            echo 'Calling '.$method.'() method, which sets a new
    cookie with an expiration of '.$expTime.' seconds.';
            return;
        }
    }

    As shown above, the “CookieSaver” class looks nearly identical to the previous examples, except that now the class provides a concrete implementation of the “__call()” method. As you can see, the generic structure of this method is the following:

    function __call($method,$arguments){
        // method definition goes here
    }

    The skeleton of this method should give you a clearer idea, with reference to its input parameters. Yep, you guessed right; when this method is called, it will take up the name of the initial method that was invoked along with an array containing its arguments. Here’s an example that shows the complete process for overloading a method call, which results in the triggering of the previous “_call()” method. Please study the code listed below:

    // overload 'CookieSaver' class and implement the __call() method
    overload('CookieSaver');
    // instantiate 'CookieSaver' object
    $cookieSaver=&new CookieSaver();
    // set cookie via __call() method
    @$cookieSaver->setExpTime('exp1','exp2','exp3');

    Things are getting exciting now! If you analyze the above script, you’ll notice the following line:

    @$cookieSaver->setExpTime('exp1','exp2','exp3');

    What I’m doing here basically is overloading a method call, in this case “setExpTime()”, in order to trigger the corresponding “__call()” method. As you noticed, I’m using a nonexistent method to do this, so I coded the “@” error suppressor before the expression.

    Of course, I have to admit the code isn’t elegant at all, but it does trigger the “__call()” method, as shown below:

    Calling setexptime() method, which sets a new cookie with an
    expiration of 900 seconds.

    As you saw, the example above shows a crude implementation of overloading a method call, which results in the pertinent “__call()” method being triggered by the PHP parser. Again, I’d like to emphasize that the example focuses on demonstrating the functionality of method call overloading, to the detriment of the code's elegance.

    Wrapping up

    Over this second part of the series I showed you how to combine the “__set()” and “__get()” methods inside the same class, in order to trigger them when a pair of property accesses are overloaded. Also, you learned how to overload a method call, which triggers the corresponding “__call()” method. Since class overloading isn’t natively supported in PHP 4, I used some code workarounds to keep things working properly.

    In the last tutorial, I’ll teach you how to implement object overloading in PHP 5. See you in the next part!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 3 hosted by Hostway
    Stay green...Green IT