Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PHP8.2 Disjunctive Normal Form Types with a default value of null #9655

Closed
@HypeMC

Description

@HypeMC

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

https://3v4l.org/iBlne

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.

https://3v4l.org/k0OjD

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

https://3v4l.org/UADDP

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

https://3v4l.org/gjOAp

PHP Version

PHP 8.2rc3

Operating System

Ubuntu 22.04

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp