PHP  
downloads | documentation | faq | getting help | | php.net sites | links 
search for in the  
previousob_startoverloadnext
Last updated: Tue, 28 May 2002
view this page in Printer friendly version | English | Brazilian Portuguese | Czech | Dutch | French | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Turkish

LXXIV. Object property and method call overloading

Varoitus

Varoitus! T�m� laajennus on EKSPERIMENTAALINEN. T�m�n laajennuksen ominaisuudet, k�ytt�ytyminen ja funktioiden nimet ja parametrit voivat muuttua varoituksetta seuraavissa PHP versioissa.

The purpose of this extension is to allow overloading of object property access and method calls. Only one function is defined in this extension, overload() which takes the name of the class that should have this functionality enabled. The class named has to define appropriate methods if it wants to have this functionality: __get(), __set() and __call() respectively for getting/setting a property, or calling a method. This way overloading can be selective. Inside these handler functions the overloading is disabled so you can access object properties normally.

Some simple examples on using the overload() function:

Esimerkki 1. Overloading a PHP class

<?php

class OO
{
    var $a = 111;
    var $elem = array('b' => 9, 'c' => 42);

    // Callback method for getting a property
    function __get($prop_name, &$prop_value)
    {
        if (isset($this->elem[$prop_name])) {
            $prop_value = $this->elem[$prop_name];
            return true;
        } else {
            return false;
        }
    }

    // Callback method for setting a property
    function __set($prop_name, $prop_value)
    {
        $this->elem[$prop_name] = $prop_value;
        return true;
    }
}

// Here we overload the OO object
overload('OO');

$o = new OO;
print "\$o->a: $o->a\n"; // print: $o->a:
print "\$o->b: $o->b\n"; // print: $o->b: 9
print "\$o->c: $o->c\n"; // print: $o->c: 42
print "\$o->d: $o->d\n"; // print: $o->d:

// add a new item to the $elem array in OO
$o->x = 56; 

// instantiate stdclass (it is built-in in PHP 4)
// $val is not overloaded!
$val = new stdclass;
$val->prop = 555;

// Set "a" to be an array with the $val object in it
// But __set() will put this in the $elem array
$o->a = array($val);
var_dump($o->a[0]->prop);

?>

Varoitus

As this is an experimental extension, not all things work. There is no __call() support currently, you can only overload the get and set operations for properties. You cannot invoke the original overloading handlers of the class, and __set() only works to one level of property access.

Sis�llys
overload -- Enable property and method call overloading for a class
User Contributed Notes
Object property and method call overloading
add a note about notes

20-Jan-2002 07:33

I think that object references should be implicit when using objects.

---
$my_object->child = $other_object;

$foo              = $my_object->child;

$my_object->bar ( $other_object );
---

would be more interesting than

---
$my_object->child =& $other_object;

$foo =& $my_object->child;

class MyClass
{
 ...

 function bar ( &$object ) {...}

 ...
};
---

__get, __set and __call will probably introduce a solution to this
problem.

class MyClass
{

    /**
     * When accessing a class member, we return a reference
     * to the member if it's an object. Else, we return a
     * copy.
     */
    function __get ($var_name, &$return)
    {
        if ( is_object($this->$var_name) )
        {
            $return =& $this->$var_name;
        }
        else
        {
            $return = $this->$var_name;
        }
        return true;
    }

    /**
     * It seems that "__set()" does not take a reference to a
     * variable as second parameter (cf: overload() example).
     * But if &$value is possible, the following code should
     * work.
     */
    function __set ($var_name, &$value )
    {
        if ( is_object($value) )
        {
            $this->$var_name =& $value;
        }
        else
        {
            $this->$var_name = $value;
        }
        return true;
    }

};

overload ("MyClass");

The __call() method should also give a way to get parameters' references
if they are objects.

The problem :

$foo = $my_object->foo;

$foo is not a reference to $my_object->foo;

If the right member of the '=' sign is a reference, it would be great if
'=' do the same job as '=&'.

ben at dubuque365 dot com
13-Jun-2002 04:06

The docs don't say this yet, probably because it goes without saying, but
if you want to try this extension you have to add
"--enable-overload" to your configure args.

If you've got the time, please help test this extension. Even without
__call, the benefits of this extension are huge. PHP class designers can
finally control how class properties are accessed and modified. 

One thing you may want to note is that the __get and __set methods won't
be called if the named property is already defined within the object.

It looks like Andrei decided this after he wrote the documentation. This
means that part of the example above is no longer correct. It should
actually read:

print: $o->a: 111

01-Jul-2002 07:00
mmmmmmmmmmm mmmmmmmmmmmmmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm
mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm
mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm
mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm
mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm mmmmmmmmmmm

add a note about notes
previousob_startoverloadnext
Last updated: Tue, 28 May 2002
show source | credits | stats | mirror sites:  
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat Jul 6 00:05:55 2002 CEST