note 114740 added to reflectionclass.setstaticpropertyvalue
| From: | AndrewJackson | Date: | Mon, 31 Mar 2014 22:53:30 +0000 |
| Subject: | note 114740 added to reflectionclass.setstaticpropertyvalue | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199084@lists.php.net to get a copy of this message | ||
Calling this method on a static property that is not public will return a ReflectionException
stating the property does not exist. This is quite misleading as the property is valid.
class test {
public static $publicProperty = 'public';
private static $privateProperty = 'private';
public static function printProperties() {
echo self::$publicProperty . "\n";
echo self::$privateProperty . "\n";
}
}
$reflectedClass = new \ReflectionClass('test');
$reflectedClass->setStaticPropertyValue('publicProperty', 'foo');
$reflectedClass->setStaticPropertyValue( 'privateProperty', 'bar' );
PHP Fatal error: Uncaught exception 'ReflectionException' with message 'Class test
does not have a property named privateProperty'
If you retrieve the method using the reflection class getProperty method you can circumnavigate this
issue
$reflectedProperty = $reflectedClass->getProperty('privateProperty');
$reflectedProperty->setAccessible(true);
$reflectedProperty = $reflectedProperty->setValue('bar');
test::printProperties();
will echo
foo
bar
----
Server IP: 72.52.91.14
Probable Submitter: 81.109.60.249
----
Manual Page -- http://php.net/manual/en/reflectionclass.setstaticpropertyvalue.php
Edit -- https://master.php.net/note/edit/114740
Del: integrated -- https://master.php.net/note/delete/114740/integrated
Del: useless -- https://master.php.net/note/delete/114740/useless
Del: bad code -- https://master.php.net/note/delete/114740/bad+code
Del: spam -- https://master.php.net/note/delete/114740/spam
Del: non-english -- https://master.php.net/note/delete/114740/non-english
Del: in docs -- https://master.php.net/note/delete/114740/in+docs
Del: other reasons-- https://master.php.net/note/delete/114740
Reject -- https://master.php.net/note/reject/114740
Search -- https://master.php.net/manage/user-notes.php