Bug #72194 [Opn]: unset() on declared class properties undeclares it partially

From: Date: Thu, 28 Sep 2017 14:52:00 +0000
Subject: Bug #72194 [Opn]: unset() on declared class properties undeclares it partially
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-211421@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72194&edit=1 ID: 72194 Updated by: cmb@php.net Reported by: pierre dot rineau at makina-corpus dot com Summary: unset() on declared class properties undeclares it partially Status: Open Type: Bug Package: Reflection related Operating System: Any PHP Version: Irrelevant Block user comment: N Private report: N New Comment: Related to bug #64497. Previous Comments: ------------------------------------------------------------------------ [2017-07-12 13:55:37] sean at m2mobi dot com This issue currently prevents me from using unittests for cleanup functions. I can't check if a protected class property has been unset because Reflection reports it as an "undefined property" afterwards. If it was null before however isset() will report FALSE before and after the unset and property_exists() will report TRUE before and after. This means I have no way to check if my unset happened, therefore I see this behaviour as a bug in reflection as the properties still exist. ------------------------------------------------------------------------ [2016-05-25 12:25:35] pierre dot rineau at makina-corpus dot com If you say that the unset()'d property becomes "undefined" and not null, then there is a logical problem: as a developer, there is absolutely no way writing PHP code to determine the difference between "undefined" and "null", then I cannot make my code more robust. In my opinion, this very edge case behaviour is a bug. ------------------------------------------------------------------------ [2016-05-15 14:46:38] bwoebi@php.net The property (as in property declaration) still exists. Only dynamic properties can completely vanish. The correct construct checking for existence of the property *value* is isset(). Note that unset() sets the value to undefined [not null!], but does not affect the existence of the property declaration if it is a declared one. class A { public $b; } $a = new A; var_dump(property_exists($a, "b")); // true var_dump(isset($a->b)); // false (as $a->b is null) var_dump($a->b); // null var_dump(property_exists($a, "c")); // false $a->c = 1; var_dump(property_exists($a, "c")); // true unset($a->c); var_dump(property_exists($a, "c")); // false unset($a->b); var_dump(property_exists($a, "b")); // true var_dump(isset($a->b)); // false (as $a->b is undefined) var_dump($a->b); // null and emits a notice ($a->b is undefined) It's true that you can trick code using it (wrongly); that code probably should be using isset(). property_exists() is really only meant to check the existence of declaration or it actually having a value. ------------------------------------------------------------------------ [2016-05-12 13:19:29] pierre dot rineau at makina-corpus dot com Yes, it is either one of: - property_exists() should not return true for unset() properties - unset() un-declares statically declared class properties ------------------------------------------------------------------------ [2016-05-11 13:58:06] nikic@php.net To clarify, the "bug" you are reporting here is that property_exists() returns true for properties that have been unset? ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=72194 -- Edit this bug report at https://bugs.php.net/bug.php?id=72194&edit=1

« previous php.bugs (#211421) next »