我试图调用静态类变量中的函数,但中出现了一些问题
这是的最小示例
#!/usr/bin/php
<?php
class Foo {
public static $func;
public static function call() {
echo "calling func'n";
if ( is_callable(self::$func))
self::$func();
else echo "no call'n";
}
}
Foo::call();
Foo::$func = function() { echo "hello'n"; };
Foo::call();
?>
它给了我以下输出
calling func
no call
calling func
PHP Notice: Undefined variable: func in /home/edwin/hola.php on line 10
PHP Fatal error: Function name must be a string in /home/edwin/hola.php on line 10
将self::$func
的引用存储在变量中:
public static function call() {
echo "calling func'n";
$callable = self::$func; // store in a variable
if ( is_callable($callable))
$callable();
else
echo "no call'n";
}