php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71731 Null coalescing operator and ArrayAccess
Submitted: 2016-03-07 15:30 UTC Modified: -
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: matej21 at matej21 dot cz Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 7.0.4 OS:
Private report: No CVE-ID: None
 [2016-03-07 15:30 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-03-20 12:53 UTC] nikic@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=a175aa9dcaed5e295d015ff73c663e06c2335155
Log: Fixed bug #71731
 [2016-03-20 12:53 UTC] nikic@php.net
-Status: Open +Status: Closed
 [2016-07-20 11:32 UTC] davey@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=a175aa9dcaed5e295d015ff73c663e06c2335155
Log: Fixed bug #71731
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 07:01:29 2024 UTC