PHP
  Home arrow PHP arrow Page 2 - 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 property access: using the “__set()” method


    (Page 2 of 4 )

    As you’ll see shortly, PHP 4 comes with the “overload()” function and allows both overloading of object property access and method calls. Once a class has been defined, it’s possible to access and set a property via the “__set()” and “__get()” methods respectively, and even overload a property access via a method call, through the homonymous “__call()” method.

    Perhaps this sounds a little confusing to you. Allow me to implement a simple example, which hopefully will help to clarify how class overloading works in PHP 4. To begin with, I’ll define a sample class, which I imaginatively called “CookieSaver.” After defining this class, I’ll show you how property access can be overloaded via the “__set()” method.

    So first , here’s the source code of the “CookieSaver()” class:

    // define 'CookieSaver' class and implement __set() 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 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 an expiration of
    '.$expTime.' seconds.';
            return;
        }
    }

    As shown above, the logic of the “CookieSaver” class is pretty straightforward. Essentially, what it does is set a cookie by using the “$cookieName” and “$value” properties respectively, which are used within the “setCookie()” method. Inversely, the value of this cookie can be retrieved by the pertinent “getCookie()” method, if you’re patient enough and study the code listed above.

    So far, the definition of the previous class falls under what you usually expect from a typical class. However, slow down and pay attention to the following method:

    // 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 an expiration of '.$expTime.'
    seconds.';
        return;
    }

    As you can see, the above “__set()” method has been defined, in order to first, modify the value of one specific array element included within the $expTimes property, and second, set a new cookie that takes up this value as its expiration time.

    Indeed, what makes this method interesting isn’t how the cookie is set, but how the value of the elements that compose the $expTimes array can be modified through overloading a property access. To put it simply, if you create a class that implements a concrete definition for the “__set()” method, this will be automatically called with the following parameters:

    __set($property,$value){
        // method definition goes here
    }

    However, to make sure this method will be properly called, the class must not contain the property that you’re trying to access. In this example, this condition is met, since the “__set()” method only modifies the elements of the $expTimes array, and not this property directly. Of course, this requirement must be satisfied when using the “__get()” and “__call()” methods too, as you’ll see later on.

    All right, now that you know how the “__set()” method has been correctly defined, it’s time to see how a property access can be overloaded. In order to achieve this, PHP 4 uses its “overload()” function, which will be explained in detail in the next section. Therefore, click on the link below and keep reading.

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