我很好奇为什么在类变量上使用范围解析运算符会导致致命的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
?>
PHP有百般无聊的规则/臭虫“这一点,以后不能去”。你只需要使用一个变量。 – Ryan
'{$ test - > reference} :: output();'?大括号总是很好,当有潜在的歧义... – DaveRandom
它工作,如果你尝试'($测试 - >参考)::输出()'? – secretformula