Re: Re: Function variables in classes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 11/1/07, Sebastian Hopfe <s.hopfe@xxxxxxx> 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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux