asd*_*fjk -2 php type-hinting php-7
我正在使用 php7 的类型提示。所以我有以下代码
class Uni
{
/**
* @var Student
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Student")
* @ORM\JoinColumn(nullable=false)
*/
private $student;
function _construct(){
$this->student= new Student();
}
/**
* Set student
*
* @param \AppBundle\Entity\Student $student
*
* @return Uni
*/
public function setStudent (Student $student): Uni
{
$this->student= $student;
return $this;
}
/**
* Get student
*
* @return \AppBundle\Entity\Student
*/
public function getStudent(): Student
{
return $this->student;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试为 Uni 加载新表单时,出现此错误
Type error: Return value of AppBundle\Entity\Uni::getStudent() must be an instance of AppBundle\Entity\Student, null returned
Run Code Online (Sandbox Code Playgroud)
我怎样才能摆脱这个错误?我找到了一个可以为空的解决方案,它需要 php 7.1。但现在我必须坚持使用 php 7.0。那么我该如何解决这个问题呢?
您的构造函数中有一个错字,两个下划线必须在 construct.
function __construct() {
$this->student = new Student();
}
Run Code Online (Sandbox Code Playgroud)
正因为如此,$student是null在你的对象被创建的时候。
| 归档时间: |
|
| 查看次数: |
5456 次 |
| 最近记录: |