D
D
deepin2019-07-26 12:56:42
PHP
deepin, 2019-07-26 12:56:42

Should I specify that the property is an array?

protected static $routes = [];// работает и просто protected static $routes;
public $vars = [];// работает и просто public $vars;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2019-07-26
@deepin

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 = nullone, 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 question

Ask a Question

731 491 924 answers to any question