And can I therefore safely refactor all instances of
class Blah
{
// ...
private $foo = null;
// ...
}
to
class Blah
{
// ...
private $foo;
// ...
}
?
Simple answer, yes. See http://php.net/manual/en/language.types.null.php
The special NULL value represents a variable with no value. NULL is the only possible value of type null.
You can easily test by performing a var_dump()
on the property and you will see both instances it will be NULL
class Blah1
{
private $foo;
function test()
{
var_dump($this->foo);
}
}
$test1 = new Blah1();
$test1->test(); // Outputs NULL
class Blah2
{
private $foo = NULL;
function test()
{
var_dump($this->foo);
}
}
$test2 = new Blah2();
$test2->test(); // Outputs NULL
PHP 7.4 adds typed properties which do not default to null
by default like untyped properties, but instead default to a special "uninitialised" state which will cause an error if the property is read before it is written. See the "Type declarations" section on the PHP docs for properties.