Bug #71731 [Opn->Csd]: Null coalescing operator and ArrayAccess
Edit report at https://bugs.php.net/bug.php?id=71731&edit=1
ID: 71731
Updated by: nikic@php.net
Reported by: matej21 at matej21 dot cz
Summary: Null coalescing operator and ArrayAccess
-Status: Open
+Status: Closed
Type: Bug
Package: Scripting Engine problem
PHP Version: 7.0.4
Block user comment: N
Private report: N
New Comment:
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=a175aa9dcaed5e295d015ff73c663e06c2335155
Log: Fixed bug #71731
Previous Comments:
------------------------------------------------------------------------
[2016-03-07 15:30:22] matej21 at matej21 dot cz
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 this bug report at https://bugs.php.net/bug.php?id=71731&edit=1
Thread (2 messages)