假如有个person类,代码如下:
class Person{
public $name = 'lightWay';
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)
{
return $this->$name;
}
}
通过反射(Reflection)机制:
$person = new Person();
$obj = new ReflectionClass(get_class($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}\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}";
echo $v->getValue($person) ? "\t=\t{$v->getValue($person)}" : '', ";\n";
}
echo "\n";
//if(is_array($Methods)) ksort($Methods);
foreach ($Methods as $k => $v)
{
echo "\tfunction {$k} () {}; \n";
}
echo "}\n";
查看下结果:
就这么愉快窥探到类的内部了哦