Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

And can I therefore safely refactor all instances of

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

to

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

?

like image 994
Jesse Avatar asked Dec 31 '12 03:12

Jesse


People also ask

What is the default value of null?

The value produced by setting all value-type fields to their default values and all reference-type fields to null . An instance for which the HasValue property is false and the Value property is undefined. That default value is also known as the null value of a nullable value type.

What is the default value of the property?

The default value is the value specified in the HTML value attribute. The difference between the defaultValue and value property, is that defaultValue contains the default value, while value contains the current value after some changes have been made.

What do you understand by default value and null value?

null means that some column is empty/has no value, while default value gives a column some value when we don't set it directly in query.

What is the default value of column for which no default value is defined?

Using NULL as a default value If you specify no default value for a column, the default is NULL unless you place a NOT NULL constraint on the column. In this case, no default value exists. If you specify NULL as the default value for a column, you cannot specify a NOT NULL constraint as part of the column definition.


1 Answers

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.

like image 176
cryptic ツ Avatar answered Sep 20 '22 17:09

cryptic ツ