From:             me at fixxxer dot me
Operating system: any
PHP version:      5.4Git-2013-03-23 (snap)
Package:          Reflection related
Bug Type:         Bug
Bug description:ReflectionProperty::setValue triggers __set for unset property

Description:
------------
I'm not sure it's a bug. And if it is, it's a low priority bug.

But current behavior is illogical: ReflectionProperty represents an
existing 
property, and it is logical to assume it never triggers magic methods.

The case is a bit weird. :) We've had a discussion on phpclub.ru forums (in

Russian, http://phpclub.ru/talk/threads/75432/page-2) about dealing with
the 
case when a property has been unset() and there are magic methods present.

When you unset a property, it is visible as undefined ("Undefined property"

notice), and if there is a __set method, it starts being triggered when
setting 
te same property. But property_exists() returns true for this property, so
it 
means the property is still there in internal object structures - it is not
the 
same as it has never been defined. During the discussion on restoring the 
property without triggering __set, I came up with an idea to use 
ReflectionProperty. But it ended up with a strange result shown in the test

script.

My point is that ReflectionProperty::setValue should never trigger __set.

Test script:
---------------
class C {

    protected $foo;

    public function test() {
        var_dump('Setting value with ReflectionProperty::setValue...');
        $foo = (new ReflectionClass($this))->getProperty('foo');
        $foo->setAccessible(true);
        $foo->setValue($this, 'foo'); // sets value, ok
        $foo->setAccessible(false);

        var_dump($this->foo);

        var_dump('Unsetting...');
        unset($this->foo);
        var_dump($this->foo);

        var_dump('Setting value with ReflectionProperty::setValue
again...');
        $foo = (new ReflectionClass($this))->getProperty('foo');
        $foo->setAccessible(true);
        $foo->setValue($this, 'foo'); // triggers __set() !
        $foo->setAccessible(false);

        var_dump($this->foo);
    }

    public function __set($k, $v) {
        var_dump("__set triggered: [$k] = '$v'");
    }

}

(new C)->test();

Expected result:
----------------
string(50) "Setting value with ReflectionProperty::setValue..."
string(3) "foo"
string(12) "Unsetting..."

Notice: Undefined property: C::$foo in /home/build/tmp/1.php on line 20
NULL
string(56) "Setting value with ReflectionProperty::setValue again..."
string(3) "foo"


Actual result:
--------------
string(50) "Setting value with ReflectionProperty::setValue..."
string(3) "foo"
string(12) "Unsetting..."

Notice: Undefined property: C::$foo in /home/build/tmp/1.php on line 20
NULL
string(56) "Setting value with ReflectionProperty::setValue again..."
string(30) "__set triggered: [foo] = 'foo'"

Notice: Undefined property: C::$foo in /home/build/tmp/1.php on line 28
NULL

-- 
Edit bug report at https://bugs.php.net/bug.php?id=64497&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=64497&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=64497&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=64497&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=64497&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=64497&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=64497&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=64497&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=64497&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=64497&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=64497&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=64497&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=64497&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=64497&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=64497&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=64497&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=64497&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=64497&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=64497&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=64497&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=64497&r=mysqlcfg

Reply via email to