And can I therefore safely refactor all instances of
class Blah
{
// ...
private $foo = null;
// ...
}
to
class Blah
{
// ...
private $foo;
// ...
}
?
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
You can easily test by performing a
var_dump()
on the property and you will see both instances it will beNULL
Yes
as per the docs:null
is a concept of a variable that has not been set to any particular value. It is relatively easy to make mistakes when differentiating betweennull
and empty1 values.private $foo = null;
is exactly equivalent toprivate $foo;
. In both cases the class attribute is defined with a value ofnull
.isset
will correctly returnfalse
for both declarations of$foo
;isset
is the boolean opposite ofis_null
, and those values are, as per above,null
.For reference, I recommend reviewing the PHP type comparison tables.
1: In this case I am referring to typed values that return true for the
empty
function, or which are otherwise considered "falsey". I.E.null
,0
,false
, the empty array (array()
), and the empty string (''
).'0'
is also technically empty, although I find it to be an oddity of PHP as a language.