Edit report at http://bugs.php.net/bug.php?id=54620&edit=1

 ID:                 54620
 User updated by:    jeroen at asystance dot nl
 Reported by:        jeroen at asystance dot nl
 Summary:            ReflectionClass::setStaticPropertyValue does not
                     work for protected/private
 Status:             Open
 Type:               Bug
 Package:            Reflection related
 Operating System:   linux
 PHP Version:        5.3.6
 Block user comment: N
 Private report:     N

 New Comment:

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 :)


Previous Comments:
------------------------------------------------------------------------
[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

Reply via email to