Magic methods + Reflection API = can't investigate class properties. Why?
If I use magic methods. While using reflection API, I can't investigate class properties.. Why is it so?
EDIT
What is Reflection API? pls do not refer me php.net i didnt understo开发者_如何学JAVAod that.. guide me in your words plsss
Using magic methods to access properties, those properties will generally not be present in the class' definition.
Your class' definition will generally look like this :
class MyClass {
private $data;
public function __get($name) {
return $this->data[$name];
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
}
As there is no real properties -- there is on only a $data
array, which will be used by the magic methods __get
an __set
as a big data-store -- those cannot be seen by the Reflection API.
That's one of the problems caused by using magic methods : they are used to access properties (or methods, with __call
) which are not there -- and the Reflection API can only see what's there.
A possible solution might be to increase the scope of $data to protected:
class MyClass {
protected $data;
public function __get($name) {
return $this->data[$name];
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
}
This way, extended classes can access the array as they see fit and collect runtime defined properties.
精彩评论