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 12While 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.
Fields 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...