note 24347 deleted from language.oop.newref by danbrown
| From: | danbrown@php.net | Date: | Sat, 07 Nov 2009 19:11:21 +0000 |
| Subject: | note 24347 deleted from language.oop.newref by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162591@lists.php.net to get a copy of this message | ||
Note Submitter: rpav at NOSPAMusers dot sf dot net
----
Ah HA! I just found an easy workaround with a bit of playing. This works:
--- cut ---
$OBJECTS = array();
class Object {
function &Object() {
global $OBJECTS;
$OBJECTS[] = &$this;
}
function _init() {
}
}
class Derived {
function &Derived() {
Object::Object();
}
}
function blah() {
global $FOO;
$x =& new Derived;
$GLOBALS['FOO'] = &$x;
$x->foo = 42;
display("x", $x);
}
function display($text, &$o) {
echo("($text) =>" . serialize($o));
}
blah();
display("GLOBALS[FOO]", $GLOBALS['FOO']);
display("OBJECTS", $OBJECTS);
--- cut ---
As you can see, simply adding & before the constructor to make it return a reference does the
trick. The bit about "returning a copy by default" lead me to try this trick, and it
seems to work. Have fun!
-rjp