And can I therefore safely refactor all instances of
class Blah
{
// ...
private $foo = null;
// ...
}
to
class
Apart from the accepted answer that is still true, with the addition of typed properties in PHP 7.4 there are new things to consider.
Given the following code
class A {
public ?DateTime $date;
}
$a = new A();
$a->date;
You will get the error
PHP Fatal error: Uncaught Error: Typed property A::$date must not be accessed before initialization
That means that the property $date
is not automatically initialized with null
even though this would be a valid value. You'll have to initialize it directly like public ?DateTime $date = null;
or use the constructor to do so.