使用file_get_contents声明私有变量时出错

时间:2014-10-10 14:16:28

标签: php file class

加载php文件后我的HTML-soure为空。

有人可以告诉我为什么在使用file_get_contents声明私有变量时出现此错误?

class Main {
    private $order = file_get_contents("file.ini");
    ...
    ...
    ...
}

我使用的是PHP 5.3.28,是的,allow_url_fopen = On

谢谢!

2 个答案:

答案 0 :(得分:1)

这是不可能的。在构造方法中声明它。

class Main {
  private $order;
  public function __construct(){
    $this->order = file_get_contents("file.ini");
  }
}

答案 1 :(得分:0)

我希望这会对你有所帮助:

class Main {

    private $order;

    public function __construct($path) {
        $this->order = file_get_contents($path);
    }

    public function getOrder() {
        return $this->order;
    }

    public function setOrder($path) {
        $this->order = file_get_contents($path);
    }

}
祝你好运!

相关问题