note 24347 added to language.oop.newref

From: Date: Tue, 13 Aug 2002 19:13:12 +0000
Subject: note 24347 added to language.oop.newref
Groups: php.notes 
Request: Send a blank email to php-notes+get-34953@lists.php.net to get a copy of this message
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 -- http://www.php.net/manual/en/language.oop.newref.php http://master.php.net/manage/user-notes.php?action=edit+24347 http://master.php.net/manage/user-notes.php?action=delete+24347 http://master.php.net/manage/user-notes.php?action=reject+24347

« previous php.notes (#34953) next »