Is a property default value of null the same as no default value?

后端 未结 3 591
死守一世寂寞
死守一世寂寞 2021-01-17 07:43

And can I therefore safely refactor all instances of

class Blah
{
    // ...
    private $foo = null;
    // ...
}

to

class         


        
相关标签:
3条回答
  • 2021-01-17 08:06

    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.

    0 讨论(0)
  • 2021-01-17 08:20

    Untyped properties

    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
    

    Typed properties

    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.

    0 讨论(0)
  • 2021-01-17 08:22

    Is a property default value of null the same as no default value?

    Yes

    as per the docs:

    The special NULL value represents a variable with no value.

    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 between null and empty1 values.

    private $foo = null; is exactly equivalent to private $foo;. In both cases the class attribute is defined with a value of null.

    isset will correctly return false for both declarations of $foo; isset is the boolean opposite of is_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.

    0 讨论(0)
提交回复
热议问题