我想从同一类中的另一个静态方法中获取静态方法的引用,但是PHP总是将这两行(参见下面的示例代码)解释为对常量的访问。在PHP中不可能获得对静态方法的引用吗?
class foo
{
public static function test()
{
self::bar(); // calling (not referencing) works
$bar_reference = self::bar; // Error: Undefined class constant 'bar'
}
public static function bar()
{
echo "hello";
}
}
foo::test();
$bar_reference = foo::bar; // Error: Undefined class constant
再次澄清:我不想调用静态方法-我只想获得它的引用
你可以创建一个可调用的
class foo
{
public static function test()
{
$bar_reference = array(__CLASS__, 'bar');
// Call it
$bar_reference();
}
public static function bar()
{
echo "hello";
}
}
foo::test();
$bar_reference = foo::bar();