php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54620 ReflectionClass::setStaticPropertyValue does not work for protected/private
Submitted: 2011-04-28 12:45 UTC Modified: 2011-05-21 20:03 UTC
Votes:2
Avg. Score:3.0 ± 2.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: jeroen at asystance dot nl Assigned:
Status: Not a bug Package: Reflection related
PHP Version: 5.3.6 OS: linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jeroen at asystance dot nl
New email:
PHP Version: OS:

 

 [2011-04-28 12:45 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-28 16:25 UTC] 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 16:58 UTC] 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 17:20 UTC] 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-05-21 20:03 UTC] felipe@php.net
-Status: Open +Status: Bogus
 [2011-05-21 20:03 UTC] felipe@php.net
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.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Jul 16 00:01:26 2019 UTC