Skip to content

empty() returns true for magic method __get() results #19036

Closed as not planned
Closed as not planned
@hibipo-ro

Description

@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

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

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions