Home arrow PHP arrow Page 2 - Implementing Property Overloading in PHP 4

Overloading a property access: using the “__set()” method - PHP

The object-oriented paradigm includes the ability to overload classes; not surprisingly, this is possible in PHP. In this first part of a series, you will learn how to implement class overloading in PHP 4, specifically using the “overload()” function, which comes in handy for triggering “__set()” and “__get()” methods when a property access is overloaded.

TABLE OF CONTENTS:
  1. Implementing Property Overloading in PHP 4
  2. Overloading a property access: using the “__set()” method
  3. Overloading a PHP 4 class: using the native “overload()” function
  4. Going deeper into property access overloading: using the “__get()” method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
July 11, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: