douxun2018 2019-01-28 13:48
浏览 82

PHP未声明的静态属性

I have class defined like this:

class FileSystem
{
    private static $_instance = NULL;

    private function __construct()
    {
        ...
    }

    public static function getInstance()
    {
        if (self::$_instance === NULL) { // <- line 83
            self::$_instance = new FileSystem;
        }

        return self::$_instance;
    }

    ...
}

The error I get when running it is:

Uncaught Error: Access to undeclared static property: FileSystem::$_instance in /var/www/html/.../FileSystem.php:83

Server on which the code is running has php 7.1.

Method that calls that class is in another class:

public function stream_close()
{
    if (!is_resource($this->_stream)) {
        return false;
    }

    FileSystem::getInstance()->fclose($this->_stream);

    return true;
}
  • 写回答

1条回答 默认 最新

  • doushi1912 2019-01-28 14:24
    关注

    your code as posted in your question should work fine. There is one reason of this kind of error and it's when you don't declare you static attribute so your error must be something like you forgot to save or that you have commented that declaration and so on.

    评论

报告相同问题?

悬赏问题

  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退