On my symfony 5 project, I am in the process of migrating from PHP 7.3 to 7.4.
Now I can type all my properties.
In my Entities files, PhpStorm automatically typed all my properties for me, but I very often end up with this kind of error :
Typed property App\Entity\User::$name must not be accessed before initialization
I have looked through several sites that talk about this problem, like:
https://github.com/doctrine/orm/issues/7944
But I don't have a real answer on what I really need to do, some people seem to contradict each other.
So if I understand correctly, I had to go from :
private ?string $name;
to
private ?string $name = null;
And the same for all the $id properties, I should put :
private ?int $id = null;
It's correct ?
Now, should I reproduce this system for absolutely all of my properties which are nullable?
But that's not all, I also have properties which are not supposed to be nullable, but which the setters and getters still offered this possibility.
And PhpStorm automatically types these properties to me as nullable. Should I also initialize them with a null value?
For DateTime? I initialize them to null value too?
Relations between Entities?
And finally, the collections? Before I used ArrayCollection, and now I have to type them with Collection, like this :
private Collection $absences;
But before that, in the constructor, I had this:
public function __construct()
{
$this->absences = new ArrayCollection();
}
Should I keep this?
Thank you very much for all of your responses !