Bug #74478 [Opn]: null coalescing operator failing with SplFixedArray
Edit report at https://bugs.php.net/bug.php?id=74478&edit=1
ID: 74478
User updated by: dariusz dot ruminski at gmail dot com
Reported by: dariusz dot ruminski at gmail dot com
Summary: null coalescing operator failing with SplFixedArray
Status: Open
Type: Bug
Package: SPL related
Operating System: Windows
PHP Version: 7.1.4
Block user comment: N
Private report: N
New Comment:
https://3v4l.org/BudKa
Previous Comments:
------------------------------------------------------------------------
[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
Thread (4 messages)