php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74478 null coalescing operator failing with SplFixedArray
Submitted: 2017-04-20 11:56 UTC Modified: 2017-04-20 16:08 UTC
From: dariusz dot ruminski at gmail dot com Assigned: jhdxr (profile)
Status: Closed Package: SPL related
PHP Version: 7.1.4 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dariusz dot ruminski at gmail dot com
New email:
PHP Version: OS:

 

 [2017-04-20 11:56 UTC] 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

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-04-20 12:00 UTC] dariusz dot ruminski at gmail dot com
https://3v4l.org/BudKa
 [2017-04-20 16:08 UTC] jhdxr@php.net
-Status: Open +Status: Assigned
 [2017-04-20 16:08 UTC] jhdxr@php.net
-Assigned To: +Assigned To: jhdxr
 [2017-05-24 16:02 UTC] nikic@php.net
Automatic comment on behalf of jhdxr
Revision: http://git.php.net/?p=php-src.git;a=commit;h=872e43d6e55e4af84681b259198ee688287cd40d
Log: Fixed bug #74478
 [2017-05-24 16:02 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Aug 09 02:05:51 2022 UTC