note 90255 added to language.oop5.reflection
| From: | richardcook at gmail dot com | Date: | Sun, 12 Apr 2009 21:39:08 +0000 |
| Subject: | note 90255 added to language.oop5.reflection | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-153129@lists.php.net to get a copy of this message | ||
the newInstanceArgs function cannot call a class' constructor if it has references in its
arguments, so be careful what you pass into it:
<?php
class Foo {
function __construct (&$arr) {
$this->arr = &$arr;
}
function createInstance () {
$reflectionClass = new ReflectionClass("Bar");
return $reflectionClass->newInstanceArgs(array($this, $this->arr));
}
function mod($key, $val) {
$this->arr[$key] = $val;
}
}
class Bar {
function __construct (&$foo, &$arr) {
$this->foo = &$foo;
$this->arr = &$arr;
}
function mod($key, $val) {
$this->arr[$key] = $val;
}
}
$arr = array();
$foo = new Foo($arr);
$arr["x"] = 1;
$foo->mod("y", 2);
$bar = $foo->createInstance();
$bar->mod("z", 3);
echo "<pre>";
print_r($arr);
print_r($foo);
print_r($bar);
echo "</pre>";
/*
Output:
Warning: Invocation of Bar's constructor failed in [code path] on line 31
Fatal error: Call to a member function mod() on a non-object in [code path] on line 58
*/
?>
----
Server IP: 208.69.120.35
Probable Submitter: 74.129.99.190
----
Manual Page -- http://www.php.net/manual/en/language.oop5.reflection.php
Edit -- https://master.php.net/note/edit/90255
Del: integrated -- https://master.php.net/note/delete/90255/integrated
Del: useless -- https://master.php.net/note/delete/90255/useless
Del: bad code -- https://master.php.net/note/delete/90255/bad+code
Del: spam -- https://master.php.net/note/delete/90255/spam
Del: non-english -- https://master.php.net/note/delete/90255/non-english
Del: in docs -- https://master.php.net/note/delete/90255/in+docs
Del: other reasons-- https://master.php.net/note/delete/90255
Reject -- https://master.php.net/note/reject/90255
Search -- https://master.php.net/manage/user-notes.php