Re: Re: Function variables in classes
| From: | Nathan Nobbe | Date: | Thu, 01 Nov 2007 14:08:02 +0000 |
| Subject: | Re: Re: Function variables in classes | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-263916@lists.php.net to get a copy of this message | ||
On 11/1/07, Sebastian Hopfe <s.hopfe@gmx.net> wrote:
>
> I think you should log it, because it seems to be, and you found this
> error.
i would not consider this a bug.
what paul is asking about is the variable function syntax in php.
http://www.php.net/manual/en/functions.variable-functions.php
whats happening is php is not resolving the first portion of the variable
contents
to a class name, nor is it capable of resolving the scope resolution syntax
when
dynamically evaluating a variable contents in the context of a method call.
there is no mention of such support in the manual.
consider this fragament (it will not work)
class DynamicMethodCaller {
public function memFunc() {
echo __METHOD__ . PHP_EOL;
}
public function invoker($dynamicMethod) {
$dynamicMethod();
}
}
$instance = new DynamicMethodCaller();
$instance->invoker('$this->memFunc');
now consider this revision (which works perfectly)
class DynamicMethodCaller {
public function memFunc() {
echo __METHOD__ . PHP_EOL;
}
public function invoker($dynamicMethod) {
$this->$dynamicMethod();
}
}
$instance = new DynamicMethodCaller();
$instance->invoker('memFunc');
the only difference between this fragment and the one
originally posted is the use of static member functions in the
original post. here are 2 fragments showing what works
and what doesnt when working with static class member functions
(doest work)
class DynamicMethodCaller {
static public function memFunc() {
echo __METHOD__ . PHP_EOL;
}
public function invoker($dynamicMethod) {
$dynamicMethod();
}
}
$instance = new DynamicMethodCaller();
$instance->invoker('DynamicMethodCaller::memFunc');
(works)
class DynamicMethodCaller {
public function memFunc() {
echo __METHOD__ . PHP_EOL;
}
public function invoker($dynamicMethod) {
self::$dynamicMethod();
}
}
$instance = new DynamicMethodCaller();
$instance->invoker('memFunc');
in general the use of eval() should be avoided unless absolutely necessary.
in this case it is not necessary; just use the syntax supported by the
interpreter
and youre good to go.
-nathan