虽然我知道可以通过多种方法调用静态方法,例如:
A::staticFunction();
或者
$class = 'A';
$class::staticFunction();
或者
$a = new A(); // Assume class has been defined elsewhere
$a->staticFunction();
但是,有人可以解释为什么以下方法不起作用,如果可能的话,如何使这项工作(不求助于提供的解决方案):
// Assume object $b has been defined & instantiated elsewhere
$b->funcName::staticFunction(); // Where funcName contains the string 'A'
这会产生以下 PHP 解析错误:
解析错误:语法错误,意外的 '::' (T_PAAMAYIM_NEKUDOTAYIM)
典型的工作解决方案(遵循第二种方法)(如果可能,最好避免):
// Assume object $b has been defined & instantiated elsewhere
$funcName = $b->funcName; // Where funcName contains the string 'A'
$funcName::staticFunction();