[英]Php error, Cannot access self:: when no class scope is active
People report having this issue on a website of mine. 人们在我的网站上报告有此问题。 But the code goes like:
但是代码如下:
$class = get_called_class();
$instance = new \ReflectionClass($class);
$class::$List = array_flip($instance->getConstants()); // Error here
I absolutely never get that error myself. 我绝对绝对不会出错。
Does somebody know what's happening? 有人知道发生了什么吗?
get_called_class()
must be called inside the class itself. 必须在类本身内部调用
get_called_class()
。 I guess you are not seeing errors because you have error_reporting(0)
or similar on your localhost. 我猜您没有看到错误,因为您的本地主机上有
error_reporting(0)
或类似名称。
class Test {
static public function test() {
echo get_called_class(); //OK
}
}
not 不
<?
echo get_called_class(); //Not OK
?>
Update . 更新 。 Eureka!
尤里卡!
$class = get_called_class();
$instance = new \ReflectionClass($class);
$class::$List = array_flip($instance->getConstants()); // Error here
should be 应该
...
$instance::List = array_flip($instance->getConstants());
You are trying to set a static non-existing value on a string. 您试图在字符串上设置静态不存在的值。
get_called_class()
return a string, not an object, and certainly not a static class
. get_called_class()
返回字符串,而不是对象,当然也不是static class
。
在某些版本的PHP 5.3.x上,getConstants似乎已损坏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.