Bug #71731 [NEW]: Null coalescing operator and ArrayAccess
From: matej21 at matej21 dot cz
Operating system:
PHP version: 7.0.4
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Null coalescing operator and ArrayAccess
Description:
------------
When I use null coalescing operator on a class implementing ArrayAccess
then offsetExists is not called, but offsetGet is called instead (which
may trigger some error). It should be equivalent to ternary operator
with isset.
https://3v4l.org/Xbojv
There is a similar issue with __get and __isset methods
https://bugs.php.net/bug.php?id=71359
Test script:
---------------
<?php
$data = 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 "isset:\n";
isset($data['foo']) ? NULL : NULL;
echo "null coalescing operator:\n";
$data['foo'] ?? NULL;
Expected result:
----------------
isset:
offsetExists
null coalescing operator:
offsetExists
Actual result:
--------------
isset:
offsetExists
null coalescing operator:
offsetGet
--
Edit bug report at https://bugs.php.net/bug.php?id=71731&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71731&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71731&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71731&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=71731&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=71731&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=71731&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=71731&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=71731&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=71731&r=support
Expected behavior: https://bugs.php.net/fix.php?id=71731&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=71731&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=71731&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=71731&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71731&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=71731&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=71731&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=71731&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71731&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=71731&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=71731&r=mysqlcfg
Thread (2 messages)
- matej21 at matej21 dot cz