PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links 
search for in the  
previousob_startoverloadnext
Last updated: Tue, 03 Sep 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | French | German | Hungarian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

LXXV. Propriet� object e method call overloading

Attenzione

Questo modulo � SPERIMENTALE. Ovvero, il comportamento di queste funzioni, i nomi di queste funzioni, in definitiva tutto ci� che � documentato qui pu� cambiare nei futuri rilasci del PHP senza preavviso. Siete avvisati, l'uso di questo modulo � a vostro rischio.

Lo scopo di questa estensione � di permettere l'overloading delle propriet� di accesso agli oggetti e dei metodi di chiamata. Solo una funzione � definita in questa estensione, overload() che prende il nome dalla classe che ha questa funzionalit� abilitata. La classe nominata ha da definire metodi appropriati se vuole avere questa funzionalit�: __get(), __set() and __call() rispettivamente per ricevere/impostare una propriet�, o chiamare un metodo. Questa strada del sovraccarico pu� essere selettiva. Dentro queste funzioni handler l'overloading � disabilitato cos� si pu� accedere alle propriet� dell'oggetto normalmente.

Alcuni semplici esempi sull'uso della funzione overload()

Esempio 1. Overloading di una classe PHP

<?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);

?>

Attenzione

Siccome � una estensione sperimentale, non tutto funziona. Non c'� attualmente il supporto per __call(), puoi solo overloadare le operazioni di ricezione e di impostazione per le propriet�. Non puoi invocare l'overloading handlers originale della classe, e __set() funziona solo su un livello di propriet� di accesso.

Sommario
overload -- Abilita le propriet� e il method call overloading per una classe
User Contributed Notes
Propriet� object e method call overloading
add a note about notes
[email protected]
20-Jan-2002 08: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 05: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

add a note about notes
previousob_startoverloadnext
Last updated: Tue, 03 Sep 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Fri Sep 6 19:51:45 2002 CEST