php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79911 isset - error when using array acess on stdClass
Submitted: 2020-07-28 23:14 UTC Modified: 2020-07-28 23:49 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: gregorio at bonfante dot dev Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
31 + 37 = ?
Subscribe to this entry?

 
 [2020-07-28 23:14 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-07-28 23:17 UTC] requinix@php.net
-Status: Open +Status: Feedback -Type: Feature/Change Request +Type: Bug -Package: Scripting Engine problem +Package: *General Issues
 [2020-07-28 23:17 UTC] 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:43 UTC] gregorio at bonfante dot dev
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

"""
 [2020-07-28 23:49 UTC] requinix@php.net
-Status: Feedback +Status: Not a bug
 [2020-07-28 23:49 UTC] requinix@php.net
The best place to talk about changing this would be on the internals mailing list. Because it's not the sort of thing that can simply be changed.
https://www.php.net/mailing-lists.php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 23:01:29 2024 UTC