Bug #79911 [Com]: isset - error when using array acess on stdClass
| From: | gregorio at bonfante dot dev | Date: | Tue, 28 Jul 2020 23:43:53 +0000 |
| Subject: | Bug #79911 [Com]: isset - error when using array acess on stdClass | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-228261@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79911&edit=1
ID: 79911
Comment by: gregorio at bonfante dot dev
Reported by: gregorio at bonfante dot dev
Summary: isset - error when using array acess on stdClass
Status: Feedback
Type: Bug
Package: *General Issues
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
I don't think it is a bug, but it is odd behavior when considering the behavior with an arrays,
resources or scalars.
For example
"""
<?php
$null = null;
$array = [];
$string = 'string';
$int = 0;
$resource = curl_init();
var_dump(isset($undef->x->x->x)); // bool(false)
var_dump(isset($null->x->x->x)); //bool(false)
var_dump(isset($array->x->x->x)); //bool(false)
var_dump(isset($string->x->x->x)); //bool(false)
var_dump(isset($int->x->x->x)); //bool(false)
var_dump(isset($resource->x->x->x)); //bool(false)
var_dump(isset($undef['x']['x']['x'])); //bool(false)
var_dump(isset($null['x']['x']['x'])); //bool(false)
var_dump(isset($array['x']['x']['x'])); //bool(false)
var_dump(isset($string['x']['x']['x'])); //bool(false)
var_dump(isset($int['x']['x']['x'])); //bool(false)
var_dump(isset($resource['x']['x']['x'])); //ON 7.4 bool(false) ON 8
Uncaught Error: Cannot use object of type CurlHandle as array
$obj = new stdClass();
var_dump(isset($obj->x->x->x)); //bool(false)
var_dump(isset($obj['x'])); // Uncaught Error: Cannot use object of type stdClass as array
"""
Previous Comments:
------------------------------------------------------------------------
[2020-07-28 23:17:22] requinix@php.net
Why do you think this is a bug? Using [] syntax is specifically for arrays and objects implementing
ArrayAccess.
------------------------------------------------------------------------
[2020-07-28 23:14:39] gregorio at bonfante dot dev
Description:
------------
Similar to https://bugs.php.net/bug.php?id=55223
Not a bug, but still not what I expected.
Using array access results in an error if the subject is an object without ArrayAccess.
Not sure about the solution as I don't have familiarity with the PHP internals.
I was able to adjust this behavior adding
"""
else if(!check_empty) {
return 0;
}
"""
to 'zend_std_has_dimension' on Zend/zend_object_handlers.c
before the "else { zend_bad_array_access(ce); return 0; }"
Test script:
---------------
<?php
$a = [];
$o = new stdClass();
var_dump(isset($a->prop));
var_dump(isset($o[0]));
Expected result:
----------------
bool(false)
bool(false)
Actual result:
--------------
bool(false)
Fatal error: Uncaught Error: Cannot use object of type stdClass as array in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79911&edit=1