将file_get_contents用于class属性时出错

时间:2015-03-17 16:15:03

标签: php class

我收到错误消息“#39;('在尝试在课堂上执行此操作时:

private $doglist = file_get_contents('dogdropdown.html');

出于某种原因这是不允许的吗? 我也试过使用这样的函数:

public function getDogList(){
$list = file_get_contents('dogdropdown.html');
return $list;

} 

也没有用。如果我使用它包括它但不会在正确的位置包含它。

1 个答案:

答案 0 :(得分:2)

声明类属性时,只能为应引用对象的变量分配基本标量值或null。如果属性需要保存某些操作的结果,则要么将其设置为静态,要么将结果分配给构造函数或类的方法。

为了做你想做的事,你需要以下内容:

class MyClass {
    private $doglist;

    function __construct() {
        $this->doglist = file_get_contents('dogdropdown.html');
    }

}
相关问题