【发布时间】:2020-04-03 13:04:55
【问题描述】:
我已经更新了我的类定义以利用新引入的属性类型提示,如下所示:
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 上,它们都是可为空的属性。
【问题讨论】:
标签: php doctrine-orm php-7 type-hinting php-7.4