反射API demo

<?php
    /**
     * reflection.php       反射API
     * ========================================
     * @Copyright			(C) 2013  GCMS
     * @Author				guhao
     * @Date		    	13-4-24
     * ========================================
     * @Version             1.0
     */
class person{
    public $name;
    public $gender;
    public function say(){
        echo $this->name,"\tis",$this->gender,"\r\n";
    }
    public function __set($name,$value){
        echo "$name $value \r\n";
        $this->$name=$value;
    }
    public function __get($name){
        if(!isset($this->name)){
            echo "未设置";
            $this->$name="正在为您设置默认值";
        }
        return $this->$name;
    }
}
$student=new person();
$student->name="Tom";
$student->gender="男";
$student->age=24;

//反射获取类的原型
$obj=new ReflectionClass('person');
$className=$obj -> getName();
$Methods = $Properties = array();
foreach($obj->getProperties() as $val){
    $Properties[$val->getName()] = $val;
}
foreach($obj->getMethods() as $val){
    $Methods[$val->getName()] = $val;
}
echo "class {$className}\n{\n";
is_array($Properties)&&ksort($Properties);

foreach($Properties as $k => $v){
    echo "\t";
    echo $v->isPublic() ? 'public' : '',$v->isPrivate ? 'private' : '',$v->isProtected() ? 'protected'  : '',$v->isStatic() ? 'static' : '';
    echo "\t{$k}\n";
}
echo "\n";
if(is_array($Methods)) ksort($Methods);
foreach($Methods as $k => $v){
    echo "\t fonction {$k} () {} \n";
}
echo "}\n";

 

你可能感兴趣的:(demo)