Bug #81147 [Opn]: getDefaultValue() throws if a mandatory parameter follows
| From: | krakjoe@php.net | Date: | Wed, 16 Jun 2021 18:18:37 +0000 |
| Subject: | Bug #81147 [Opn]: getDefaultValue() throws if a mandatory parameter follows | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-18862@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81147&edit=1
ID: 81147
Updated by: krakjoe@php.net
Reported by: me at derrabus dot de
Summary: getDefaultValue() throws if a mandatory parameter
follows
Status: Open
Type: Bug
-Package: Reflection related
+Package: Documentation problem
Operating System: macOS 11.4, Ubuntu 20.04
PHP Version: 8.1.0alpha1
Block user comment: N
Private report: N
New Comment:
Because the parameter is implicitly nullable, no deprecation warning in shown in this case.
However the behaviour does not change, and the deprecation warning reads:
> Optional parameter $arg1 declared before required parameter $arg2 is implicitly treated as a
> required parameter
In other words, the default value is ignored, and the engine generates ZEND_RECV not RECV_INIT,
leading to internal error in Reflection.
There's nothing to fix here, but this should be documented.
Previous Comments:
------------------------------------------------------------------------
[2021-06-16 18:02:37] me at derrabus dot de
Description:
------------
If I reflect on a function that has an optional parameter before a mandatory one and I try to fetch
the default value of the optional parameter, a ReflectionException is raised. This was not the case
on PHP 8.0 and earlier.
I am unsure if this is an intentional change because specifying a function where a mandatory
parameter follows an optional one had already been deprecated in PHP 8.0. But I somewhat expected
that the deprecation would not be turned into an error before PHP 9.
Test script:
---------------
<?php
function myFunctionA(object $arg1 = null)
{
}
function myFunctionB(object $arg1 = null, $arg2)
{
}
var_dump((new ReflectionParameter('myFunctionA', 0))->getDefaultValue());
var_dump((new ReflectionParameter('myFunctionB', 0))->getDefaultValue());
Expected result:
----------------
NULL
NULL
Actual result:
--------------
NULL
Fatal error: Uncaught ReflectionException: Internal error: Failed to retrieve the default value
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81147&edit=1