[php-src] Issue #10497: Cannot directly modify an object stored in a constant
| From: | claudepache | Date: | Fri, 03 Feb 2023 21:00:45 +0000 |
| Subject: | [php-src] Issue #10497: Cannot directly modify an object stored in a constant | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-243636@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/10497
Author: claudepache
### Description
The following code:
```php
<?php
const a = new stdClass;
a->b = 42;
var_dump(a);
```
Resulted in this output:
```
Fatal error: Cannot use temporary expression in write context in /tmp/preview on line 3
```
But I expected this output instead:
```
object(stdClass)#1 (1) {
["b"]=>
int(42)
}
```
(The issue also arises for objects stored in class constants.)
A workaround is to use a temp variable (because a temporary variable is not a “temporary
expression”, eh):
```php
<?php
const a = new stdClass;
$_ = a;
$_->b = 42;
var_dump(a); // it works
```
### PHP Version
PHP 8.2
### Operating System
_No response_