[php-src] Issue #8068: mysqli_fetch_object creating objects with invalid properties
| From: | ras52 | Date: | Wed, 09 Feb 2022 14:40:26 +0000 |
| Subject: | [php-src] Issue #8068: mysqli_fetch_object creating objects with invalid properties | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-239587@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8068
Author: ras52
### Description
The following code fetches a object with a single property whose name and value is '42'.
```php
<?php
$dbh = new mysqli('localhost', 'richard');
$res = $dbh->query('SELECT 42');
$obj = $res->fetch_object();
$prop = 42;
var_dump($obj);
echo ">{$obj->$prop}<\n";
$obj = json_decode(json_encode($obj));
var_dump($obj);
echo ">{$obj->$prop}<\n";
```
This results in this output:
```
object(stdClass)#3 (1) {
[42]=>
string(2) "42"
}
PHP Notice: Undefined property: stdClass::$42 in /home/richard/test.php on line 8
><
object(stdClass)#4 (1) {
["42"]=>
string(2) "42"
}
>42<
```
The first
var_dump confirms that $obj does contain the expected property.
However it is not possible to access it via the $obj->$prop syntax, nor with
$obj->{42} or $obj->{'42'}. Transforming it to JSON and
back turns it into a more regular object which can be accessed with either of these syntaxes.
I expected the >< in the output to read >42<. Changing
$res->fetch_object() to (object)$res->fetch_assoc() has the expected
effect, and provides an adequate work-around.
My guess is that this is another instance of [this bug/oddity that was fixed in PHP
7.2](https://www.php.net/manual/en/migration72.incompatible.php#migration72.incompatible.object-array-casts)
that escaped being fixed.
Just in case its relevant, I'm testing with MariaDB 10.5.12.
### PHP Version
PHP 8.1.2 (also 8.0.15 and 7.4.27)
### Operating System
Debian 11