简体   繁体   English

php错误,当没有类作用域处于活动状态时,无法访问self ::

[英]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.

相关问题 致命错误:{address}中没有活动的类范围时,无法访问self :: - Fatal error: Cannot access self:: when no class scope is active in {address} 当没有类范围处于活动状态时,无法访问self :: - Cannot access self:: when no class scope is active Codeigniter无法访问self ::当帮助器中没有活动的类范围时 - Codeigniter Cannot access self:: when no class scope is active inside a helper 什么是致命错误的解决方案:当avada主题中没有类范围活动时,不能使用“self”? - What is solution to Fatal error: Cannot use “self” when no class scope is active in avada theme? PHP致命错误:无法访问父类::当当前类范围在cakephp 3.x中没有父类时 - PHP Fatal error: Cannot access parent:: when current class scope has no parent in cakephp 3.x 致命错误:无法访问父级 :: 当当前类作用域中没有父级时 - Fatal error: Cannot access parent:: when current class scope has no parent in 无法访问父级:: 当前 class scope 没有父级时 - cannot access parent:: when current class scope has no parent php class =“活动”的导航栏错误 - php class=“active” error with navbar PHP类函数中的致命错误“无法访问空属性”在哪里? - where is the fatal error “Cannot access empty property” in the PHP class function? 使用php页面上的类处于活动状态 - Class active when on page using php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM