Edit report at http://bugs.php.net/bug.php?id=54620&edit=1
ID: 54620 Updated by: fel...@php.net Reported by: jeroen at asystance dot nl Summary: ReflectionClass::setStaticPropertyValue does not work for protected/private -Status: Open +Status: Bogus Type: Bug Package: Reflection related Operating System: linux PHP Version: 5.3.6 Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php You need to use the setAccessible() approach. Previous Comments: ------------------------------------------------------------------------ [2011-04-28 17:20:47] felipecg00 at gmail dot com My bad. I mean protecting the privates is probably the way Reflection was designed for, so it's correct behavior (not a bug). For myself, I really think that Reflection power should be left on programmer's side to decide on what to do - still one should know where to use it. But maybe getProperty() was designed just for that. The Fatal Error comes from the uncaught exception, just as any left "throw new Exception()" without a catch will cause it. ------------------------------------------------------------------------ [2011-04-28 16:58:55] jeroen at asystance dot nl Thanks for the workaround. Access modifiers are for visibility between classes, so I don't see circumventing them through Reflection as a problem. If you cast en object to an array, you also get the private / protected variables (see #44273). Your workaround (using getProperty) also works just fine. So I think the way to go is just enable access to private / protected members. Whatever the design decision though, a Fatal Error is certainly a bug :) ------------------------------------------------------------------------ [2011-04-28 16:25:19] felipecg00 at gmail dot com I can reproduce this, still I think that in this case, protecting the private and protected resources is the correct behavior. You might want to check this: $rT = new ReflectionClass( 'Tehst' ); var_dump( $s = $rT->getProperty('prot') ); var_dump( $s->isStatic() ); $s->setAccessible(1); $s->setValue("myProt"); var_dump( $s->getValue() ); ------------------------------------------------------------------------ [2011-04-28 12:45:27] jeroen at asystance dot nl Description: ------------ --- >From manual page: http://www.php.net/reflectionclass.setstaticpropertyvalue#Description --- ReflectionClass::getProperties() shows public, protected and private members, but ReflectionClass::setStaticPropertyValue throws a fatal error if a protected or private member is set. The error message says that "Class Tehst does not have a property named prot", which is plain wrong. Test script: --------------- <?php class Tehst { public static $pub = 'pub'; protected static $prot = 'prot'; private static $priv = 'priv'; } $rT = new ReflectionClass( 'Tehst' ); var_export( $rT->getProperties() ); $rT->setStaticPropertyValue( 'pub', 'myPub' ); $rT->setStaticPropertyValue( 'prot', 'myProt' ); $rT->setStaticPropertyValue( 'priv', 'myPriv' ); ?> Expected result: ---------------- array ( 0 => ReflectionProperty::__set_state(array( 'name' => 'pub', 'class' => 'Tehst', )), 1 => ReflectionProperty::__set_state(array( 'name' => 'prot', 'class' => 'Tehst', )), 2 => ReflectionProperty::__set_state(array( 'name' => 'priv', 'class' => 'Tehst', )), )pub: myPub Actual result: -------------- array ( 0 => ReflectionProperty::__set_state(array( 'name' => 'pub', 'class' => 'Tehst', )), 1 => ReflectionProperty::__set_state(array( 'name' => 'prot', 'class' => 'Tehst', )), 2 => ReflectionProperty::__set_state(array( 'name' => 'priv', 'class' => 'Tehst', )), )pub: myPub PHP Fatal error: Uncaught exception 'ReflectionException' with message 'Class Tehst does not have a property named prot' in /home/jay/public_html/alis/newfile.php:14 Stack trace: #0 /home/jay/public_html/alis/newfile.php(14): ReflectionClass->setStaticPropertyValue('prot', 'myProt') #1 {main} thrown in /home/jay/public_html/alis/newfile.php on line 14 Fatal error: Uncaught exception 'ReflectionException' with message 'Class Tehst does not have a property named prot' in /home/jay/public_html/alis/newfile.php:14 Stack trace: #0 /home/jay/public_html/alis/newfile.php(14): ReflectionClass->setStaticPropertyValue('prot', 'myProt') #1 {main} thrown in /home/jay/public_html/alis/newfile.php on line 14 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=54620&edit=1