最佳答案
我已经更新了类定义,以使用新引入的属性类型提示,如下所示:
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; }
}
但是当我试图在信条上保存我的实体时,我得到了一个错误:
Typed property must not be accessed before initialization
这不仅发生在 $id
或 $createdAt
上,也发生在 $value
或 $updatedAt
上,它们是可为空的属性。