Bug #81306 [Nab]: PHP doesn't fully evaluate new expressions passed in named arguments
| From: | raincomplain at outlook dot com | Date: | Wed, 28 Jul 2021 21:14:08 +0000 |
| Subject: | Bug #81306 [Nab]: PHP doesn't fully evaluate new expressions passed in named arguments | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-235446@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=81306&edit=1
ID: 81306
User updated by: raincomplain at outlook dot com
Reported by: raincomplain at outlook dot com
Summary: PHP doesn't fully evaluate new expressions passed in
named arguments
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
PHP Version: 8.1.0beta1
Block user comment: N
Private report: N
New Comment:
If they are equivalant(as I believe)then this is a bug.
Previous Comments:
------------------------------------------------------------------------
[2021-07-28 21:00:58] raincomplain at outlook dot com
We can't dropp the static property because then there will be no bug.
Isn't:
var_dump(getTest(new A(88), new A(55)))
equivalant to:
var_dump(getTest(o2: new A(55)))
in my test script ?
------------------------------------------------------------------------
[2021-07-28 20:44:47] nikic@php.net
This example is extremely weird. Let's make it more obvious by dropping the static property and
just dumping in the constructor:
<?php
class A {
public function __construct(int $x) {
var_dump($x);
}
}
function getTest(A $o1 = new A(88), A $o2 = new A(77)) {
}
getTest(o2: new A(55));
This prints int(55) followed by int(88), exactly as it should. First the new A(55) object is created
explicitly and passed to parameter $o2, and then new A(88) is created when filling in the default
value for parameter $o1.
------------------------------------------------------------------------
[2021-07-28 20:35:26] raincomplain at outlook dot com
Description:
------------
According to the RFC of "New in initializers":
Parameter default values are evaluated from left to right on every call to the function where the
parameter is not explicitly passed.
So calling a method by not explicitly passing a default paramater and using named arguments to call
other arguments should trigger default parameter evaluation, so far so good. However, if both
arguments are objects of the same class that modify a static property, PHP only returns the copy of
the default object and "overlooks" the other object that was passed as a named argument.
Test script:
---------------
class A
{
public static int $x = 0;
public function __construct(int $x)
{
self::$x = $x; // changing this to self::$x += $x; works as expected
}
}
function getTest(A $o1 = new A(88), A $o2 = new A(77))
{
return A::$x;
}
var_dump(getTest(o2: new A(55)))
Expected result:
----------------
int(55)
Actual result:
--------------
int(88)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81306&edit=1