Bug #78053 [NEW]: empty() when testing $obj->var with magic __get function

From: Date: Wed, 22 May 2019 22:46:58 +0000
Subject: Bug #78053 [NEW]: empty() when testing $obj->var with magic __get function
Groups: php.standards 
Request: Send a blank email to standards-+get-1443@lists.php.net to get a copy of this message
From: bjorn dot macintosh at gmail dot com Operating system: Mac PHP version: 7.1.29 Package: PHP Language Specification Bug Type: Bug Bug description:empty() when testing $obj->var with magic __get function Description: ------------ php -v PHP 7.1.23 (cli) (built: Feb 22 2019 22:08:13) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies ---- Created an object with a protected property Created a magic get "__get($name) { return $this->name; }" Assigned my variable a value within the class (in correct scope) When testing if variable is empty outside of object scope it incorrectly returns true for emptyness Test script: --------------- ``` var_dump($obj->var, empty($obj->var)); // Outputs: // $obj->var = "something" // empty($obj-<var) = true But it will work if I pre-retrieve the variable. $val = $obj->var var_dump($var, empty($var)); // Outputs: // $obj->var = "something" // empty($obj-<var) = false ``` Expected result: ---------------- empty on protected variable with a getter should resolve the getter first, then test emptyness Actual result: -------------- empty call on a protected $obj->var - with a magic __get - returns true even if variable is not empty -- Edit bug report at https://bugs.php.net/bug.php?id=78053&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=78053&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=78053&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=78053&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=78053&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=78053&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=78053&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=78053&r=needscript Try newer version: https://bugs.php.net/fix.php?id=78053&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=78053&r=support Expected behavior: https://bugs.php.net/fix.php?id=78053&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=78053&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=78053&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=78053&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=78053&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=78053&r=dst IIS Stability: https://bugs.php.net/fix.php?id=78053&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=78053&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=78053&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=78053&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=78053&r=mysqlcfg

« previous php.standards (#1443) next »