代码
<?php
class person{
public $name;
public $gender;
public function say(){
echo $this->$name," \tis ",$this->$gender,"\r\n";
}
public function __set($name,$value){
$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='male';
$student->age=24;
$obj = new ReflectionClass('person');
$className = $obj->getName();
$Methods = $Properties = array();
foreach ($obj->getProperties() as $v) {
$Properties[$v->getName()] = $v;
}
foreach($obj->getMethods() as $v){
$Methods[$v->getName()] = $v;
}
echo "class {$className}<br>{<br>";
is_array($Properties)&&ksort($Properties);
foreach ($Properties as $k => $v) {
echo $v->isPublic()?'public':'',$v->isPrivate()?'private':'',
$v->isProtected()?'protected':'',
$v->isStatic()?'static':'';
echo "\t{$k}<br>";
}
echo "\n";
if(is_array($Methods)) ksort($Methods);
foreach ($Methods as $k => $v) {
echo "\tfunction {$k}(){}<br>";
}
echo "}<br>";
运行结果
class person
{
public gender
public name
function __get(){}
function __set(){}
function say(){}
}
参考书籍
《php核心技术与最佳实践》