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

From: Date: Wed, 25 May 2016 12:25:41 +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-201267@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 User updated by: pierre dot rineau at makina-corpus dot com 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: 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. Previous Comments: ------------------------------------------------------------------------ [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? ------------------------------------------------------------------------ [2016-05-11 11:16:03] pierre dot rineau at makina-corpus dot com https://3v4l.org/3sAs5 ------------------------------------------------------------------------ [2016-05-11 11:12:50] pierre dot rineau at makina-corpus dot com Changed title. ------------------------------------------------------------------------ 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 (#201267) next »