2016-12-07 116 views
0

我正在使用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; 
    } 

} 

现在,当我尝试加载统一新形式,我得到这个错误

Type error: Return value of AppBundle\Entity\Uni::getStudent() must be an instance of AppBundle\Entity\Student, null returned 

我怎样才能摆脱这种错误的?我发现了一个可空的解决方案,它需要php 7.1。但现在我必须坚持使用PHP 7.0。那我该如何解决这个问题?

回答

4

在构造函数中有一个错字,两个下划线必须在construct之前。

function __construct() { 
    $this->student = new Student(); 
} 

正因为如此,$studentnull创建你的对象时。

相关问题