Req->Bug #73242 [Opn->Nab]: empty($obj->protectedPropery) does not __get the property value

From: Date: Tue, 04 Oct 2016 14:53:19 +0000
Subject: Req->Bug #73242 [Opn->Nab]: empty($obj->protectedPropery) does not __get the property value
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-204445@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73242&edit=1 ID: 73242 Updated by: requinix@php.net Reported by: touzfik10 at gmail dot com Summary: empty($obj->protectedPropery) does not __get the property value -Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug Package: Class/Object related Operating System: windows PHP Version: 7.0.11 Block user comment: N Private report: N New Comment: empty will first confirm the existence of the variable, and the default behavior is to check whether the variable is actually defined (and accessible) on the object. That's not the case for yours so empty returns true. To override that behavior you must implement __isset. http://php.net/manual/en/language.oop5.overloading.php#object.isset Before: https://3v4l.org/meEFH After: https://3v4l.org/1mYgv You should consider doing __set too or else calling code could define variables on the object. It could just throw an exception to prevent setting values if you wanted. Really, for nearly every class out there __get/set/isset should come as a package deal - all or none. Previous Comments: ------------------------------------------------------------------------ [2016-10-04 14:24:03] touzfik10 at gmail dot com Description: ------------ I think empty($expression) is supposed to evaluate $expression before it checks if it is empty. In the code bellow, empty($obj->protectedProperty) is supposed to call __get before checking if it's empty. That makes sense, right? Well, I don't think it's happening. Sorry if I am missing something. But I think this behavior should change. Test script: --------------- $fooList = [ new Foo, new Foo ]; $bar = new Bar($fooList); var_dump(empty($bar->fooList)); // true $fooList = $bar->fooList; var_dump(empty($fooList)); // false class Foo { } class Bar { protected $fooList = []; public function __construct(array $fooList) { $this->fooList = $fooList; } public function __get(string $property) { return $this->$property; } } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=73242&edit=1

« previous php.bugs (#204445) next »