Php 类型错误:返回值必须是实例,返回null
我正在使用php7的类型提示。所以我有以下代码Php 类型错误:返回值必须是实例,返回null,php,php-7,type-hinting,Php,Php 7,Type Hinting,我正在使用php7的类型提示。所以我有以下代码 class Uni { /** * @var Student * * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Student") * @ORM\JoinColumn(nullable=false) */ private $student; function _construct(){ $this->stud
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;
}
}
现在,当我尝试为Uni加载新表单时,出现了以下错误
Type error: Return value of AppBundle\Entity\Uni::getStudent() must be an instance of AppBundle\Entity\Student, null returned
我怎样才能消除这个错误?我找到了一个带有nullable的解决方案,它需要PHP7.1。但现在我必须坚持使用PHP7.0。那么我该如何解决这个问题呢?你的构造函数有一个输入错误,前面必须有两个下划线 因此,创建对象时,
$student
为null
function __construct() {
$this->student = new Student();
}