我正在制作新的ReflectionClass
,然后将受保护的属性_products
设置为可访问。总是回归null
我在这里做错了吗?我在5.4.11
$project = new ReflectionClass( $instance_of_object );
$property = $project->getProperty( '_products' );
$property->setAccessible( true );
$products = $property->getValue( $project );
我正在尝试确保在单元测试中正确设置属性...
答案 0 :(得分:2)
我准备了一个简单的例子。如果您可以执行它,则代码中的其他位置必定存在错误:
class The_Class {
private $_products;
public function __construct() {
$this->_products = 'foo';
}
}
$instance_of_class = new The_Class();
$reflClass = new ReflectionClass($instance_of_class);
$member = $reflClass->getProperty('_products');
$member->setAccessible(true);
// Here is an error in your code:
// Note that I'm using $instance_of_class, rather then
// $reflClass as argument to getValue()
var_dump($member->getValue($instance_of_class)); // string(3) "foo"