[php-src] Issue #19036: empty() returns true for magic method __get() results
| From: | hibipo-ro | Date: | Sat, 05 Jul 2025 00:22:30 +0000 |
| Subject: | [php-src] Issue #19036: empty() returns true for magic method __get() results | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-250377@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/19036
Author: hibipo-ro
### Description
## Description
The
empty() function returns true when called on a property accessed
through the __get() magic method, even though the property contains a non-empty value.
## Expected Behavior
empty($accessor->name) should return false because the property
contains the string "Tokyo".
## Actual Behavior
empty($accessor->name) returns true, which is unexpected.
## Code to Reproduce
```php
<?php
class Accessor
{
public function __construct(public $target) {}
public function __get($key)
{
return $this->target->$key;
}
}
$school = new stdClass();
$school->name = "Tokyo";
$accessor = new Accessor($school);
var_dump($accessor->name); // "Tokyo"
var_dump(strcmp($accessor->name, $accessor->target->name)); // 0
var_dump($accessor->name === $accessor->target->name); // true
var_dump(empty($accessor->name)); // true - WHY?
var_dump(empty($accessor->target->name)); // false
$name = $accessor->name;
var_dump(empty($name)); // false
```
## Tested on
- PHP 8.1
- PHP 8.2
- PHP 8.3
### PHP Version
```plain
PHP 8.2.28 (cli) (built: May 21 2025 23:17:46) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.28, Copyright (c) Zend Technologies
with Xdebug v3.4.3, Copyright (c) 2002-2025, by Derick Rethans
```
### Operating System
_No response_