Bug #72194 [NEW]: unset() on declared class properties triggers warnings when accessing it later
| From: | pierre dot rineau at makina-corpus dot com | Date: | Wed, 11 May 2016 11:11:50 +0000 |
| Subject: | Bug #72194 [NEW]: unset() on declared class properties triggers warnings when accessing it later | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-201008@lists.php.net to get a copy of this message | ||
From: pierre dot rineau at makina-corpus dot com
Operating system: Any
PHP version: Irrelevant
Package: Reflection related
Bug Type: Bug
Bug description:unset() on declared class properties triggers warnings when accessing it later
Description:
------------
If you declare a class with declared properties, you might want to use
unset() to reset the property value to null, but when accessing it
later, it causes a "undefined property" warning.
This seems legit, but it becomes tricky once you notice that
property_exists() returns true before calling unset(), which is the
wanted behavior since the property is declared (even when
uninitialized), but it also returns true after calling unset(), which is
incompatible with the "undefined property" warning.
It seems to always have worked this way, but any code using
property_exists() will be tricked by the false positive.
[someuser@localhost] ~/current
> php -v
PHP 5.5.25 (cli) (built: May 14 2015 10:02:12)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend
Technologies
with Xdebug v2.4.0RC2, Copyright (c) 2002-2015, by Derick Rethans
[someuser@localhost] ~/current
> php -a
Interactive shell
php > class A { public $b; };
php > $a = new A;
php > echo property_exists($a, 'b');
1
php > $a->b;
php > unset($a->b);
php > echo property_exists($a, 'b');
1
php > $a->b;
PHP Notice: Undefined property: A::$b in php shell code on line 1
PHP Stack trace:
PHP 1. {main}() php shell code:0
php >
pounard@guinevere] ~
> php -v
PHP 7.0.6 (cli) (built: May 1 2016 07:39:38) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend
Technologies
with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans
[pounard@guinevere] ~
> php -a
PHP Warning: PHP Startup: Unable to load dynamic library
'/usr/lib/php/modules/redis.so' - /usr/lib/php/modules/redis.so:
undefined symbol: zval_used_for_init in Unknown on line 0
Interactive shell
php > class A { public $b; };
php > $a = new A;
php > echo property_exists($a, 'b');
1
php > $a->b;
php > unset($a->b);
php > echo property_exists($a, 'b');
1
php > $a->b;
PHP Notice: Undefined property: A::$b in php shell code on line 1
PHP Stack trace:
PHP 1. {main}() php shell code:0
php >
Test script:
---------------
class A { public $b; };
$a = new A;
echo property_exists($a, 'b'); // Returns '1'
$a->b; // Remains silent
unset($a->b);
echo property_exists($a, 'b'); // Still returns '1'
$a->b; // Cause a "Notice: Undefined property A::$b"
--
Edit bug report at https://bugs.php.net/bug.php?id=72194&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=72194&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=72194&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=72194&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=72194&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=72194&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=72194&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=72194&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=72194&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=72194&r=support
Expected behavior: https://bugs.php.net/fix.php?id=72194&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=72194&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=72194&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=72194&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72194&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=72194&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=72194&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=72194&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=72194&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=72194&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=72194&r=mysqlcfg