note 8052 modified in call_user_method by jmcastagnetto
| From: | jmcastagnetto@php.net | Date: | Mon, 04 Sep 2000 08:49:37 +0000 |
| Subject: | note 8052 modified in call_user_method by jmcastagnetto | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-4112@lists.php.net to get a copy of this message | ||
[Editor's note: The assertion in this note is incorrect. There is
a function to test for the existence of a particular method: method_exists().
The function described here should be used when generating programatically
generic calls to object methods, akin to lambda function calls for example]
Ok, I found a nice use for call_user_method(): Since there seems to be no function to test wether a
method exists in an object (not class! - I don't want to check all the possible parent
classes), you just call the method with @call_user_method() if you're not sure wether the
method exists. If it does not, the script just contiues (if you do $obj->method(), the script
will die with a fatal error if the method does not exist).
BUT: There's a strange limitation here: In this example, the $a property won't increase as
expected when $t->increase() is called with call_user_method(). Strange!
class test
{
var $a=0;
function increase()
{
$this->a++;
echo "a is now: ".$this->a."\n<br>";
}
}
$t = new test();
echo "Direct calls:\n<br>";
$t->increase();
$t->increase();
$t->increase();
echo "<p>\n\nIndirect calls:\n<br>";
call_user_method ("increase", $t);
call_user_method ("increase", $t);
call_user_method ("increase", $t);
http://www.php.net/manual/function.call-user-method.php