php.net |  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
 [2021-09-22 20:36 UTC] wyattbiker at gmail dot com
Description:
------------
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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-09-22 20:46 UTC] dharman@php.net
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:

<?php

$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] requinix@php.net
-Status: Open +Status: Feedback
 [2021-09-22 21:30 UTC] requinix@php.net
It's unusual but not invalid. The syntax is part of PHP's "variable variables" feature:
https://www.php.net/manual/en/language.variables.variable.php

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] requinix@php.net
-Status: Open +Status: Not a bug
 [2021-09-23 00:23 UTC] requinix@php.net
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] requinix@php.net
-Status: Open +Status: Not a bug
 [2021-09-23 00:26 UTC] requinix@php.net
Like I said, maybe an array would be better.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Oct 24 01:03:33 2021 UTC