note 88009 modified in language.oop5.reflection by danbrown

From: Date: Wed, 16 Sep 2009 16:11:24 +0000
Subject: note 88009 modified in language.oop5.reflection by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160709@lists.php.net to get a copy of this message
With PHP 5.3 protected or private properties are easy to access with setAccessible(). However, it's sometimes needed (e.g. Unit Tests) and here is a workaround for getValue(): <?php $class = new ReflectionClass('SomeClass'); $props = $class->getProperties(); // $propsStatic = $class->getStaticProperties(); $myPrivatePropertyValue = $props['aPrivateProperty']; ?> Note that it wont work if you access the property directly with getProperty(). --was-- With PHP 5.3 protected or private properties are easy to access with setAccessible(). However, it's sometimes needed (e.g. Unit Tests) and here is a workaround for getValue(): <?php $class = new ReflectionClass('SomeClass'); $props = $class->getProperties(); // $propsStatic = $class->getStaticProperties(); $myPrivatePropertyValue = $props['aPrivateProperty']; ?> Note that it wont work if you access the property directly with getProperty(). http://php.net/manual/en/language.oop5.reflection.php

« previous php.notes (#160709) next »