I am using type hinting of php7. So I have following code
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;
}
}
now when I try to load new form for Uni, I get this error
Type error: Return value of AppBundle\Entity\Uni::getStudent() must be an instance of AppBundle\Entity\Student, null returned
How can I get rid of this error? I found a solution with nullable and it requires php 7.1. But for now I have to stick with php 7.0. So how can I solve this?
You have a typo in your constructor, two underscores must come before construct
.
function __construct() {
$this->student = new Student();
}
Because of this, $student
was null
when your object was created.