0

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:

Why I am suddenly getting a "Typed property must not be accessed before initialization" error when introducing properties type hints?

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 !

eronn
  • 1,690
  • 3
  • 21
  • 53
  • [Related.](https://github.com/doctrine/orm/issues/7944) – Jeto Apr 08 '21 at 12:04
  • This is precisely the link that I checked before posting, but I did not really understand the conclusion – eronn Apr 08 '21 at 12:11
  • The conclusion is exactly as what it says in the linked duplicate. If you have typed properties, you should make sure that that the **values** of the properties match the declared types. Either you set a **valid** value on construction, or you initialize the property with a **valid** value. If you leave it uninitialized, it will fail as the error shows. – yivi Apr 08 '21 at 12:29
  • I edited my first post in more detail because I don't think it is a post duplication. I also posted this message on Reddit as you advised me Cerad – eronn Apr 08 '21 at 12:31
  • Okay, I understand a little better. But what about collections, as I explained in my first post (which I edited)? – eronn Apr 08 '21 at 12:32
  • For Doctrine collections you usually initialize as `new ArrayCollection() ` on the constructor. It's still a dupe. You need to initialize properties to a value of the correct type before accessing them. – yivi Apr 08 '21 at 12:35
  • https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/reference/best-practices.html#initialize-collections-in-the-constructor – yivi Apr 08 '21 at 12:40

0 Answers0