我很好奇为什么在类变量上使用范围解析运算符会导致致命的php错误,如果有解决方法的话。
例如:
<?php
class StaticTest
{
public static function output()
{
echo "Output called<br />";
}
}
Class Test
{
public $reference;
public function __construct()
{
$this -> reference = new StaticTest;
}
}
$static_test = new StaticTest;
$static_test::output(); //works as intended
$test = new Test;
$test -> reference::output(); //Unexpcted T_PAAMAYIM_NEKUDOTAYIM
$direct_reference = $test -> reference;
$direct_reference::output(); //works, closest solution i have found, but requires the extra line of code / variable
?>
答案 0 :(得分:3)
如果你唯一关心的是代码行数和额外变量,那么你可以在一行中完成它,而不创建任何变量:
call_user_func(array(get_class($test->reference), 'output'));
我认为,相当于:
$direct_reference = $test->reference;
$direct_reference::output();