php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62059 ArrayObject and isset are not friends.
Submitted: 2012-05-18 15:22 UTC Modified: -
Votes:3
Avg. Score:3.7 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: julien at palard dot fr Assigned:
Status: Closed Package: SPL related
PHP Version: Irrelevant OS: Linux 2.6.32-5-amd64
Private report: No CVE-ID: None
 [2012-05-18 15:22 UTC] julien at palard dot fr
Description:
------------
The doc state that isset() is not a function, so that it can do weird things 
like deep inspection, without raising a NOTICE about 'b' being undefined :

  $a = array();
  isset($a['b']['c']);

But if $a is an ArrayObject, the deep inspection does not work and a "Undefined 
index  'b'" NOTICE is triggered.

But I found a __isset() in the documentation, 
(http://www.php.net/manual/en/language.oop5.overloading.php#object.isset) it 
have a very concise documentation but it seems a sufficient convention to make 
this work :

I think that isset should call __isset() from left to right returning FALSE at 
the 1st non set member, like this :

  $a = SomeObject();
  isset($a['b']['c']['d']['e']);

should call :

  $a->__isset('b'), that should return FALSE, and stop here, wihout NOTICE.

Test script:
---------------
echo "isset on array, work without notice\n";
$array = array();
var_dump(isset($array['a']['b']));


echo "isset on arrayobject, should work as ArrayObject implements __isset\n";
$arrayobject = new ArrayObject();
var_dump(isset($arrayobject['a']['b']));


Expected result:
----------------
isset on array, work without notice
bool(false)
isset on arrayobject, should work as ArrayObject implements __isset
bool(false)


Actual result:
--------------
isset on array, work without notice
bool(false)
isset on arrayobject, should work as ArrayObject implements __isset
PHP Notice:  Undefined index:  a in test.php on line 10

Notice: Undefined index:  a in test.php on line 10
bool(false)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-23 23:05 UTC] roberts at x0 dot lv
Confirmed present in 5.3.3-7 and 5.4.7-7, and 5.4.8-1
 [2013-05-23 20:01 UTC] spneacy at gmail dot com
Confirmed in version 5.4.6

When using version 5.4.9, it appears to have been incidentally (or silently) 
fixed.
 [2014-09-03 13:08 UTC] php at mandark dot fr
Seems fix in PHP 5.5.16
 [2015-01-10 22:19 UTC] ajf at ajf dot me
Hey, it's not the ->__isset() magic method that isset() calls for indexes (array access). __isset() is used for checking property existence. You're probably thinking of ArrayAccess's ->offsetExists(), which ArrayObject does implement.

Also, this bug isn't really about ArrayObject per se, it's about ArrayAccess's offsetExists() not working with isset().
 [2015-05-13 13:31 UTC] contact dot 01834e2c at renegade334 dot me dot uk
This has only been partially fixed.

When isset($ArrayObject['foo']['bar']) is called, Zend doesn't call spl_array_has_dimension_ex(). Instead, it puts in a call to spl_array_get_dimension_ptr() with type=BP_VAR_IS. This value of type means that it doesn't generate a notice if 'foo' isn't a valid index.

However, it is worth pointing out that at a Zend level, the native offsetGet method is never actually called in this case. However, it IS called if isset() is called on an instance of a child class, which has overwritten the offsetGet method with a function that calls parent::offsetGet().

Consider the following:

<?php
class MyArrayObject extends ArrayObject {
  public function offsetGet($index) {
    // ...
    return parent::offsetGet($index);
  }
}
$ArrayObject = new ArrayObject;
$MyArrayObject = new MyArrayObject;
isset($ArrayObject['foo']['bar']);
isset($MyArrayObject['foo']['bar']);
?>

In the first case, ArrayObject::offsetGet() is NOT actually being called - Zend takes a shortcut by going straight from spl_array_read_dimension_ex() to spl_array_get_dimension_ptr(), without calling the offsetGet method.

In the second case, spl_array_read_dimension_ex() detects the presence of the overwritten offsetGet method, and calls it. This DOES call the native offsetGet method, which didn't happen in the case before.

The native offsetGet method calls spl_array_read_dimension_ex() again, BUT this time, the call is made with type=BP_VAR_R (not BP_VAR_IS as before). This means that spl_array_get_dimension_ptr() DOES generate a notice if the index does not exist.

In other words:
- isset($ArrayObject['foo']['bar']) DOES NOT generate a notice, as the native offsetGet method is never actually called by Zend.
- isset($MyArrayObject['foo']['bar']) DOES generate a notice, as it specifically calls the native offsetGet method.

This is inconsistent behaviour, and should be addressed.

$ ~/builds/php7/bin/php --version
PHP 7.0.0-dev (cli) (built: May 13 2015 14:51:36) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies
 [2016-03-20 17:19 UTC] nikic@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f3309173f916e3c5cf37910975f04310706336b5
Log: Fixed bug #62059
 [2016-03-20 17:19 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=f3309173f916e3c5cf37910975f04310706336b5
Log: Fixed bug #62059
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC