Posted by Jens on 02/15/06 14:23
Hello,
i am just trying to get familiar with the Reflection-Api.
I want to access a protected property of my class using the
getValue-method of ReflectionProperty-Class. When i execute the code i
get the Exception "Cannot access non-public member". Shouldnt it be
possible to access a property from within the same class?
Is using public getter and setter methods the only way to acces that
property?
Here is my code:
thanxx
class Test{
/**
* id *
* @access protected
* @var int
*/
protected $id = 0;
/**
* Name
*
* @access protected
* @var String
*/
protected $name = null;
protected $pkfield = null;
/**
* ...
*
*
* @access public
* @author
* @return
*/
public function __construct()
{
$this->pkfield="id";
}
/**
* Die Funktion ...
*
*
* @access public
* @author
* @return
*/
public function check()
{
try{
$rclass = new ReflectionClass(get_class());
$prop=$rclass->getProperty($this->pkfield);
//Reading Value of id
echo($this->pkfield . "=" . $prop->getValue($this));
}catch(Exception $e1){
echo("Error: " . $e1->getMessage());
}
}
}
[Back to original message]
|