I recently stumbled over reflecting private properties in PHP again. As you might know, this was not possible until now and if you tried this:
<?php
class Foo
{
private $bar = 23;
}
$obj = new Foo();
$refObj = new ReflectionObject( $obj );
$refProp = $refObj->getProperty( 'bar' );
echo $refProp->getValue( $obj );
?>
PHP thanked it to you with this:
ReflectionException: Cannot access non-public member Foo::bar in /home/dotxp/dev/ez/ezcomponents/trunk/reflection.php on line 12
While it is absolutly correct that direct access to private properties is strictly forbidden, it is quite disturbing that even reflection cannot do it when you do metaprogramming where accessing private properties can be essential. Today things changed for PHP 5.3 and 6, after Derick and me managed to convince Marcus and Derick provided a patch. You still need to explicitly state that you want to access the value of a protected/private property through reflection by the new method setAccessible() (which is a good thing to avoid people doing stupid things accedentally), but you finally get the access to it:
<?php
class Foo
{
private $bar;
}
$obj = new Foo();
$refObj = new ReflectionObject( $obj );
$refProp = $refObj->getProperty( 'bar' );
// Gather access to this properties value, although it is private
$refProp->setAccessible( true );
echo $refProp->getValue( $obj );
?>
This code should not throw an exception to you, but print the desired 23 value.
Thanks to Marcus and Derick for getting this finally done, so that we can soon forget about ugly hacks in the metaprogramming and testing area.
If you liked this blog post or learned something, please consider using flattr to contribute back: .
It's setAccessible(), not setAccesible().
Link to commentK, fixed here, too. ;)
Link to commentDoes the new method return a clone of internal objects? If not, encapsulation could be broken. For example, you could return an internal private property and modify it in a way that would break the class.
Link to commentThe first code snippet worked in 5.1.6--something was broken since then.
Link to commentNo, it does not clone anything. You can't change the value though... just read it.
Link to commentNo, objects stored in private properties are not cloned and you are correct, that encapsulation is broken by this. This is the reason for the setAccessible() method, to ensure the developer knows, what he does.
Link to comment
It seems that this does not works now:
Fatal error: Call to undefined method ReflectionProperty::setAccessible()
My version:
PHP 5.2.10-2ubuntu6.4 with Suhosin-Patch 0.9.7 (cli) (built: Jan 6 2010 22:41:56)
Someone knows how to do it now?
Oops! I see that you're using PHP5.3, so forget my previous comment, it's clear that the problem should be that this feature is not supported on PHP5.2 that's what I'm using.
Link to comment
Ahmad Al Jayousi
Philadelphia University Jordan
http://www.philadelphia.edu.jo
E-mail: aaljayousi@philadelphia.edu.jo
I think that using setAccessible(true) is a rubbish, when people are using reflection aren't they already aware of what are they doing?
Link to comment
Reflection is good for delegates implementation, not just call_user_func function that calls public class members but, the real delegates to private class members. Things like Events and EventHandlers can not be implemented without delegates and blocking this feature makes php lack huge class of OOP patterns that are common to other languages. Check
http://stackoverflow.com/questions/4584182/the-correct-way-of-doing-delegates-or-callbacks-in-php
Hi have, principally, the same approach but more bound to the concern, imho:
<?php
$reflectedProperty = new \ReflectionProperty('BareClassName', 'desiredPropertyName');
$reflectedProperty->setAccessible(true);
echo $reflectedProperty->getValue($instanciatedClass);
?>
I write this here because it took myself a bit to get the approach of the reflection; the idea is with ->getValue get the value of the reflected property *in context* to the object passed in as parameter.
I am writing a research paper and collecting information on this topic. Your post is one of the better that I have read. Thank you for putting this information into one location.
Link to commentam truly not too everyday with this dominate but I do equal to see blogs for layout ideas. You truly distended upon a mortal that I usually don't work often
Link to commentA very useful post this! Actually I was learning about the coding in php and your site has given me some of the important information to learn it easily. I am going to explore the other pages of your website to gain more information. Keep writing!
Link to commentThe more specific meaning, which is usually the first one students encounter, refers to a tendency to distinguish unfairly between different classes of people, such as people of different races <a href="http://www.bestforcasinobonus.com">www.bestforcasinobonus.com</a>
Link to commenthttp://www.bestforcasinobonus.com The more specific meaning, which is usually the first one students encounter, refers to a tendency to distinguish unfairly between different classes of people, such as people of different races
Link to commentThe more specific meaning, which is usually the first one students encounter, refers to a tendency to distinguish unfairly between different classes of people, such as people of different races
Link to commentThe author has written an excellent article. You made your point and not much to discuss. It's like this universal truth that you can not argue with the truth is not universal, everything has its exception. Thanks for this information.
Link to commentI admit with your Blog and I will be back to examine it more in the future so please keep up your work.
Link to commentThe 2013 Louis Vuitton Handbags are classic and a girl must have at least one in her closet. Chic, classic style, classy and good quality.
Link to commentI was doing a project and for that I was looking for related information. Some of the points are very useful. Do share some more material if you have.
Link to commentFields with bold names are mandatory.
Tobias Schlitt's Blog: Reflecting private properties
Tobias Schlitt has posted a handy tip about using the Reflection ...
Accessing private properties in Unit Tests
While it's absolutly correct that direct access to private properties is strictly forbidden in PHP, it's quite disturbing that even reflection cannot do it when you're writing Unit Tests. Sometimes you just want to test whether a private property contains...