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.
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.
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...