Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
I follow this logic:
- $var;
if the property will be initialized on creation (constructor, boot, etc.)
- $var = null;
if the property stores a default value that does not require initialization. From the point of view of the language, this is identical to the previous version, but seeing the explicit = null
one, I understand that this particular instance will have this value. Just makes the code easier to read.
- $var = [];
a variant of the previous one, sets a default value that will help to avoid the is_array check in the future and makes it possible to use array_push from the start, for example. Simplifies code. Well, PhpStorm will swear right away if I try to write data of a different type to this variable.
The last 2 rules apply not only to null and array, but also to other types.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question