Bug #74478 [Asn->Csd]: null coalescing operator failing with SplFixedArray
| From: | nikic@php.net | Date: | Wed, 24 May 2017 16:02:59 +0000 |
| Subject: | Bug #74478 [Asn->Csd]: null coalescing operator failing with SplFixedArray | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-209256@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=74478&edit=1
ID: 74478
Updated by: nikic@php.net
Reported by: dariusz dot ruminski at gmail dot com
Summary: null coalescing operator failing with SplFixedArray
-Status: Assigned
+Status: Closed
Type: Bug
Package: SPL related
Operating System: Windows
PHP Version: 7.1.4
Assigned To: jhdxr
Block user comment: N
Private report: N
New Comment:
Automatic comment on behalf of jhdxr
Revision: http://git.php.net/?p=php-src.git;a=commit;h=872e43d6e55e4af84681b259198ee688287cd40d
Log: Fixed bug #74478
Previous Comments:
------------------------------------------------------------------------
[2017-04-20 12:00:27] dariusz dot ruminski at gmail dot com
https://3v4l.org/BudKa
------------------------------------------------------------------------
[2017-04-20 11:56:43] dariusz dot ruminski at gmail dot com
Description:
------------
When I use null coalescing operator on a class implementing SplFixedArray then offsetExists is not
called, but offsetGet is called instead (which may trigger some error). It should be equivalent to
ternary operator with isset.
This is similar to https://bugs.php.net/bug.php?id=71731 (here -
SplFixedArray, there - ArrayAccess).
https://3v4l.org/#v714
Test script:
---------------
<?php
$arrayData = new class implements \ArrayAccess
{
public function offsetExists($offset)
{
echo "offsetExists\n";
}
public function offsetGet($offset)
{
echo "offsetGet\n";
}
public function offsetSet($offset, $value)
{
}
public function offsetUnset($offset)
{
}
};
echo "arrayData - isset:\n";
isset($arrayData['foo']) ? NULL : NULL;
echo "arrayData - null coalescing operator:\n";
$arrayData['foo'] ?? NULL;
$fixedData = new class extends \SplFixedArray
{
public function offsetExists($offset)
{
echo "offsetExists\n";
}
public function offsetGet($offset)
{
echo "offsetGet\n";
}
public function offsetSet($offset, $value)
{
}
public function offsetUnset($offset)
{
}
};
echo "fixedData - isset:\n";
isset($fixedData['foo']) ? NULL : NULL;
echo "fixedData - null coalescing operator:\n";
$fixedData['foo'] ?? NULL;
Expected result:
----------------
arrayData - isset:
offsetExists
arrayData - null coalescing operator:
offsetExists
fixedData - isset:
offsetExists
fixedData - null coalescing operator:
offsetExists
Actual result:
--------------
arrayData - isset:
offsetExists
arrayData - null coalescing operator:
offsetExists
fixedData - isset:
offsetExists
fixedData - null coalescing operator:
offsetGet
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74478&edit=1