4

I wrote simple PHP class that implements ArrayAccess Interface:

class MyArray implements ArrayAccess
{
 public $value;
 public function __construct($value = null)
 {
 $this->value = $value;
 }
 public function &offsetGet($offset)
 {
 var_dump(__METHOD__);
 if (!isset($this->value[$offset])) {
 throw new Exception('Undefined index: ' . $offset);
 }
 return $this->value[$offset];
 }
 public function offsetExists($offset)
 {
 var_dump(__METHOD__);
 return isset($this->value[$offset]);
 }
 public function offsetSet($offset, $value)
 {
 var_dump(__METHOD__);
 $this->value[$offset] = $value;
 }
 public function offsetUnset($offset)
 {
 var_dump(__METHOD__);
 $this->value[$offset] = null;
 }
}

It works normally in PHP 7, but the problem in PHP 5.6 and HHVM.

If I call function isset() on undefined index, the PHP will call offsetGet() instead of offsetExists() which will cause Undefined index notice.

In PHP 7, it calls offsetGet() only if offsetExists() returns true, so there is no error.

I think that this is related to PHP bug 62059.

The code is avalible at 3V4L, so you can see what is wrong. I added few more debug calls and throw exception if index is undefined because notices aren't shown in 3V4L: https://3v4l.org/7C2Fs

There shouldn't be any notice otherwise PHPUnit tests will fail. How can I fix this error?

asked Aug 5, 2018 at 20:14
8
  • 1
    It looks like a php bug. As a quick and dirty fix you may additionally run the check in offsetSet as well. Commented Aug 5, 2018 at 20:36
  • 1
    You can use isset to check whether the key exists before you access it. Commented Aug 5, 2018 at 20:41
  • 1
    bugs.php.net is where you report php bugs, but 5.6 won't be fixed anyway - it's not supported anymore. And is HHVM is developed at all as well? Commented Aug 5, 2018 at 20:46
  • 1
    Yep, checked it and changed my comment sorry Commented Aug 5, 2018 at 20:48
  • 1
    I think so - if php calls that method, you don't have other choice. Commented Aug 5, 2018 at 20:50

1 Answer 1

0

It looks like this is a PHP bug in old versions of PHP and HHVM. Because PHP 5.6 is not supported anymore, this bug will not be fixed.

Quick fix is to add additional check in method offsetGet() and return null if index is undefined:

class MyArray implements ArrayAccess
{
 public $value;
 public function __construct($value = null)
 {
 $this->value = $value;
 }
 public function &offsetGet($offset)
 {
 if (!isset($this->value[$offset])) {
 $this->value[$offset] = null;
 }
 return $this->value[$offset];
 }
 public function offsetExists($offset)
 {
 return isset($this->value[$offset]);
 }
 public function offsetSet($offset, $value)
 {
 $this->value[$offset] = $value;
 }
 public function offsetUnset($offset)
 {
 $this->value[$offset] = null;
 }
}

See code at 3V4L and zerkms's comments (first, second, third).

answered Aug 6, 2018 at 8:51
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.