我已经更新了类定义,以利用新引入的属性类型提示,如下所示:
class Foo {
private int $id;
private ?string $val;
private DateTimeInterface $createdAt;
private ?DateTimeInterface $updatedAt;
public function __construct(int $id) {
$this->id = $id;
}
public function getId(): int { return $this->id; }
public function getVal(): ?string { return $this->val; }
public function getCreatedAt(): ?DateTimeInterface { return $this->createdAt; }
public function getUpdatedAt(): ?DateTimeInterface { return $this->updatedAt; }
public function setVal(?string $val) { $this->val = $val; }
public function setCreatedAt(DateTimeInterface $date) { $this->createdAt = $date; }
public function setUpdatedAt(DateTimeInterface $date) { $this->updatedAt = $date; }
}
但是,当我尝试将自己的实体保存在Doctrine上时,出现一条错误消息:
初始化之前不得访问类型化的属性
这不仅发生在$id
或上$createdAt
,还发生在$value
或上$updatedAt
,这是可为空的属性。