public

ReflectionProperty::getValue and ReflectionProperty::setValue trigger PHP Magic methods

  • Download Gist
reflectionproperty_triggers_magic.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
--TEST--
Verifies that ReflectionProperty does not cause magic methods to get called when properties are unset
--FILE--
<?php
 
class Test
{
public $publicProperty;
protected $protectedProperty;
private $privateProperty;
 
public function __construct() {
unset(
$this->publicProperty,
$this->protectedProperty,
$this->privateProperty
);
}
 
function __get($name) {
echo 'called __get ' . $name;
}
 
function __set($name, $value) {
echo 'called __set ' . $name . ' ' . $value;
}
}
 
$t = new Test();
 
foreach (array('publicProperty', 'protectedProperty', 'privateProperty') as $propertyName) {
$prop = new ReflectionProperty('Test', $propertyName);
$prop->setAccessible(true);
echo var_export($prop->getValue($t), true) . "\n";
$prop->setValue($t, 'value');
}
 
?>
--EXPECTF--
NULL
NULL
NULL

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.