[英]PHP : instantiation of an object as a property of a class
i have the line of codes: 我有一行代码:
class foo{
public $object = new bar(2);
public function index(){
dd($this->object);
}
}
and the bar
object contains: 并且
bar
对象包含:
class bar{
protected $number;
function __construct($number){
$this->number = $number;
}
}
now its throwing me Constant expression contains invalid operation
现在它抛出我
Constant expression contains invalid operation
It's not currently possible to instantiate an object during class properties declaration. 当前无法在类属性声明期间实例化对象。 That should be done in the object constructor instead :
那应该在对象构造函数中完成:
class foo{
public $object;
public function __construct() {
$this->object = new bar(2);
}
public function index(){
dd($this->object);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.