|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81467 (object) casting of array should validate keynames
Submitted: 2021-09-22 20:36 UTC Modified: 2021-09-23 00:26 UTC
From: wyattbiker at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 8.0.10 OS: mac os x
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: wyattbiker at gmail dot com
New email:
PHP Version: OS:


 [2021-09-22 20:36 UTC] wyattbiker at gmail dot com
When casting an associative array to an object it should verify that in fact it is creating valid object property names.

E.g.this should produce an error.

$x=(object) array('20'=>'abc');
echo $x->20;   // << ERROR obviously

Test script:
$x=(object) array('x20'=>'abc');
echo $x->x20;   // OK

$x=(object) array('20'=>'abc');
echo $x->20;   // << ERROR obviously


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-09-22 20:46 UTC]
What is invalid about property name "20"?
I appreciate you filing the bug report, but I fail to see where the bug is. The created object is a valid object with valid property names. I can access the property without any problem:


$x=(object) array('20'=>'abc');
echo $x->{20};

// Outputs: abc

Can you explain what exactly, do you consider a bug?
 [2021-09-22 21:30 UTC]
-Status: Open +Status: Feedback
 [2021-09-22 21:30 UTC]
It's unusual but not invalid. The syntax is part of PHP's "variable variables" feature:

I'd say the solution here is that if the syntax for accessing your data via some object's properties is awkward then you shouldn't be using an object for the data. Arrays are fine too.
 [2021-09-22 23:57 UTC] wyattbiker at gmail dot com
-Status: Feedback +Status: Open
 [2021-09-22 23:57 UTC] wyattbiker at gmail dot com
Sorry didn't realize you could use the curlies. Can you close it please. Thanks
 [2021-09-23 00:23 UTC]
-Status: Open +Status: Not a bug
 [2021-09-23 00:23 UTC]
No problem. It's a very uncommonly used thing so most people don't know about it.
 [2021-09-23 00:25 UTC] wyattbiker at gmail dot com
-Status: Not a bug +Status: Open
 [2021-09-23 00:25 UTC] wyattbiker at gmail dot com
I think what threw me off is the fact that the property name is a number.  Unusual for sure.
 [2021-09-23 00:26 UTC]
-Status: Open +Status: Not a bug
 [2021-09-23 00:26 UTC]
Like I said, maybe an array would be better.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Feb 24 10:01:27 2024 UTC