Reputation: 2570
When declaring variables at the top of a class, does it matter if I do:
private $garbage_values;
instead of:
private $garbage_values = [];
Of course, there will be foreach
loops within the class that reference the array, and the array may be empty. Does this matter?
Upvotes: 1
Views: 38
Reputation: 3236
Basically, if you'd want any type of variable to work in a foreach
like it should, it should fall into the iterable
pseudo-type.
So if you use a variable that does not fall into the iterable
pseudo-type, in a foreach
an error would spring up.
And in your case, i.e. private $foo;
if you leave it like that, it basically contains the value null
which does not fall into iterable
, so it would fail.
Whereas, if you use private $foo = [];
it contains an empty array, which indeed does fall into the iterable
pseudo-type and can be used flawlessly in a foreach
.
Upvotes: 2