我已更新我的类定义,以使用新引入的属性类型提示,就像这样:
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
上。