Bug #74478 [NEW]: null coalescing operator failing with SplFixedArray
| From: | dariusz dot ruminski at gmail dot com | Date: | Thu, 20 Apr 2017 11:56:48 +0000 |
| Subject: | Bug #74478 [NEW]: null coalescing operator failing with SplFixedArray | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-208676@lists.php.net to get a copy of this message | ||
From: dariusz dot ruminski at gmail dot com
Operating system: Windows
PHP version: 7.1.4
Package: SPL related
Bug Type: Bug
Bug description:null coalescing operator failing with SplFixedArray
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 bug report at https://bugs.php.net/bug.php?id=74478&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=74478&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=74478&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=74478&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=74478&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=74478&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=74478&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=74478&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=74478&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=74478&r=support
Expected behavior: https://bugs.php.net/fix.php?id=74478&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=74478&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=74478&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=74478&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=74478&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=74478&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=74478&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=74478&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=74478&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=74478&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=74478&r=mysqlcfg