PHP
  Home arrow PHP arrow Page 2 - 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? 
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 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
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT