- Notifications
You must be signed in to change notification settings - Fork8k
Closed
Description
Description
There seems to be some inconsistent behavior with Disjunctive Normal Form Types when an argument has a default value ofnull
.
Usually if the default value isnull
, the argument is implicitly nullable, eg:
<?phpfunctionfoo(string$param =null) {var_dump($param); }functionbar(\Foo\Bar|\Foo\Baz$param =null) {var_dump($param); }foo(null);bar(null);
result:
NULLNULL
An exception to this rule are PHP 8.1 intersection types:
functionfoo(\Foo\Bar&\Foo\Baz$param =null) {var_dump($param); }foo(null);
result:
Fatal error: Cannot use null as default value for parameter $param of type Foo\Bar&Foo\Baz in /in/k0OjD on line 3Process exited with code 255.
However, the same error is raised on PHP 8.2, even though the following works:
functionfoo((\Foo\Bar&\Foo\Baz)|\Foo\Qux$param =null) {var_dump($param); }foo(null);
result:
NULL
With the introduction of DNF types, I would expect\Foo\Bar&\Foo\Baz $param = null
to implicitly mean(\Foo\Bar&\Foo\Baz)|null $param = null
, which works as expected:
functionfoo((\Foo\Bar&\Foo\Baz)|null$param =null) {var_dump($param); }foo(null);
result:
NULL
PHP Version
PHP 8.2rc3
Operating System
Ubuntu 22.04