php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76478 ReflectionParameter#allowsNull() returns false for indirect nullable decls
Submitted: 2018-06-14 16:39 UTC Modified: 2020-02-28 15:18 UTC
From: daverandom@php.net Assigned: nikic (profile)
Status: Closed Package: Reflection related
PHP Version: 7.1 OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
34 - 13 = ?
Subscribe to this entry?

 
 [2018-06-14 16:39 UTC] daverandom@php.net
Description:
------------
ReflectionParameter#allowsNull() return false for parameters where the nullability is inferred by a constant default value, where the constant has a value of NULL.

https://3v4l.org/mUePZ

Test script:
---------------
<?php

const DEFAULT_VALUE = null;

function foo(\stdClass $arg = DEFAULT_VALUE)
{
    var_dump($arg);
}

var_dump((new ReflectionFunction('foo'))->getParameters()[0]->allowsNull());
foo(null);

Expected result:
----------------
bool(true)
NULL

Actual result:
--------------
bool(false)
NULL

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-06-14 16:46 UTC] daverandom@php.net
-PHP Version: Irrelevant +PHP Version: >=5.5
 [2018-06-14 17:30 UTC] cmb@php.net
-Status: Open +Status: Verified -PHP Version: >=5.5 +PHP Version: 7.1
 [2018-06-14 17:30 UTC] cmb@php.net
It's interesting that ::getDefaultValue() returns NULL in this
case (<https://3v4l.org/R8WYD>), and that PHP < 5.6.4 didn't even
allow this function definition (likely due to bug #68446).
 [2020-02-28 15:18 UTC] nikic@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: nikic
 [2020-02-28 15:18 UTC] nikic@php.net
This has been "fixed" in PHP 8 by the removal of Schrödinger's nullability. The parameter is either nullable, or it is not. Your example will no longer be considered as nullable.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 05:01:28 2024 UTC