我正在使用 php7.1 的新功能,该功能允许在项目 Symfony 3 上返回可为 null 的类型,但在实体中使用此功能时遇到问题:
<?php
// ....
public function getCreatedBy(): ?string
{
return $this->createdBy;
}
当我使用这个函数时,我遇到了这个错误:
Type error: Return value of
Proxies\\__CG__\\NS\\ModulesBundle\\Entity\\Account::getCreatedBy()
must be of the type string, null returned
我查看了使用 Symfony 自动创建的类的代码,发现了这一点:
<?php
// ...
public function getCreatedBy(): string
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'getCreatedBy', []);
return parent::getCreatedBy();
}
如您所见,该类型不可为 null。
有人可以帮助我吗?谢谢
最佳答案
我通过将doctrine/doctrine-common库从2.6版本升级到2.8版本解决了这个问题
关于PHP 7.1 Doctrine 代理问题与可空类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46466464/