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

From: Date: Wed, 22 May 2019 22:51:38 +0000
Subject: Bug #78053 [Opn->Nab]: empty() when testing $obj->var with magic __get function
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-220954@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78053&edit=1 ID: 78053 Updated by: requinix@php.net Reported by: bjorn dot macintosh at gmail dot com Summary: empty() when testing $obj->var with magic __get function -Status: Open +Status: Not a bug Type: Bug -Package: PHP Language Specification +Package: *General Issues Operating System: Mac PHP Version: 7.1.29 Block user comment: N Private report: N New Comment: isset() and empty() try to determine the existence of a property. That is separate from retrieving its value. To provide custom support for retrieving property values you need __get. To provide custom support for determining the existence of properties you need __isset. https://www.php.net/manual/en/language.oop5.overloading.php Previous Comments: ------------------------------------------------------------------------ [2019-05-22 22:46:56] bjorn dot macintosh at gmail dot com 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 this bug report at https://bugs.php.net/bug.php?id=78053&edit=1

« previous php.bugs (#220954) next »