[PHP-NOTES] note 130508 added to reflectionparameter.allowsnull

[email protected] ("[email protected]") Sun, 12 Oct 2025 19:10:08 +0000
Newsgroups php.notes
Message-ID <[email protected]>
The note about detecting nullability on a parameter predates "?type" and other union types. Null isn't expected to be the default if there's a defined type that allows it ("?int", "mixed", "int|string|null", etc.).

<?php
function foo($bar) { }
(new ReflectionFunction('foo'))->getParameters()[0]->allowsNull();
// true

function foo(string $bar) { }
(new ReflectionFunction('foo'))->getParameters()[0]->allowsNull();
// false

function foo(?string $bar) { }
(new ReflectionFunction('foo'))->getParameters()[0]->allowsNull();
// true

function foo(?string $bar = 'baz') { }
(new ReflectionFunction('foo'))->getParameters()[0]->allowsNull();
// true (default string doesn't matter)

// Note: Raises E_DEPRECATED, implicitly making parameter nullable
function foo(string $bar = null) { }
(new ReflectionFunction('foo'))->getParameters()[0]->allowsNull();
// true (even in strict mode, PHP <= 8)
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.84.18 (proxied: 68.48.138.132)
----
Manual Page -- https://php.net/manual/en/reflectionparameter.allowsnull.php
Edit        -- https://main.php.net/note/edit/130508
Del: integrated  -- https://main.php.net/note/delete/130508/integrated
Del: useless     -- https://main.php.net/note/delete/130508/useless
Del: bad code    -- https://main.php.net/note/delete/130508/bad+code
Del: spam        -- https://main.php.net/note/delete/130508/spam
Del: non-english -- https://main.php.net/note/delete/130508/non-english
Del: in docs     -- https://main.php.net/note/delete/130508/in+docs
Del: other reasons-- https://main.php.net/note/delete/130508
Reject      -- https://main.php.net/note/reject/130508
Search      -- https://main.php.net/manage/user-notes.php