[php]代码库
<?php
class a {
public $attr = 'a';
public $x = 'q';
}
class b extends a {
public $abc = 'b';
public $attr = 'bc';
public function getVar($var){
$reflect = new ReflectionObject($this);
$properties = (array)$reflect->getProperties();
$parent = $reflect->getParentClass();
$parent_reflect = new ReflectionObject(new $parent->name());
$parent_properties = $parent_reflect->getProperties();
if($reflect){
foreach($properties as $property){
foreach($parent_properties as $parent_property){
if($property->name == $var && $property->class == $reflect->getName() && $parent_property->name == $var){
trigger_error($var . ' is finnal property');
return false;
}
}
}
}
if(!$reflect->hasProperty($var)){
trigger_error('Undefined property:' . $var);
}else{
return $this->$var;
}
}
}
$a = new b();
echo $a->getVar('attr');