Bug #81147 [Opn->Csd]: getDefaultValue() throws if a mandatory parameter follows
| From: | me at derrabus dot de | Date: | Wed, 16 Jun 2021 18:37:05 +0000 |
| Subject: | Bug #81147 [Opn->Csd]: getDefaultValue() throws if a mandatory parameter follows | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-18866@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
User updated by: me at derrabus dot de
Reported by: me at derrabus dot de
Summary: getDefaultValue() throws if a mandatory parameter
follows
-Status: Open
+Status: Closed
Type: Bug
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:
Thank you very much. <3 We can close the bug report then!
Previous Comments:
------------------------------------------------------------------------
[2021-06-16 18:33:20] nikic@php.net
Just to confirm, this is indeed an intentional change to ensure that optional-before-required is
consistently treated as a required parameter. Previously it was treated as such in nearly all
situations, but there were loopholes. One, as you have discovered, is reflection. The other is named
arguments, where it was possible to not specify such a parameter if later required parameters were
specified.
------------------------------------------------------------------------
[2021-06-16 18:33:11] me at derrabus dot de
Thank you for your very quick response, Joe!
> In other words, the default value is ignored
Yes, that is apparently what happens on PHP 8.1. However, I'd like to point out that it's
not ignored on PHP 8.0. And this is why I've opened this bug: I'd like to clarify whether
this change of behavior in 8.1 is a unintentional regression or an intentional BC break.
------------------------------------------------------------------------
[2021-06-16 18:27:46] gdgdgd at dhdhr dot hh
optional parameters before mandatory ones don't make sense at all
------------------------------------------------------------------------
[2021-06-16 18:18:37] krakjoe@php.net
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.
------------------------------------------------------------------------
[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