<?php /* PHP5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions, methods and extensions. Additionally, the reflection API offers ways to retrieve doc comments for functions, classes and methods. PHP5加入了反射机制用来获取类、方法、属性、参数等的详细信息,包括注释。 API概览 class Reflection { } interface Reflector { } class ReflectionClass implements Reflector { } class ReflectionProperty implements Reflector { } class ReflectionFunctionAbstract implements Reflector { } class ReflectionParameter implements Reflector { } class ReflectionFunction extends ReflectionFunctionAbstract implements Reflector { } class ReflectionMethod extends ReflectionFunctionAbstract implements Reflector { } class ReflectionObject extends ReflectionClass implements Refletor { } class ReflectionExtension implements Refletor { } */ class Person { private $_name; public function get_name() { return $this->_name; } public function set_name($name='') { return $this->_name = $name; } } $class = new ReflectionClass('Person'); foreach ($class->getProperties() as $property) { var_dump($property->name); } foreach ($class->getMethods() as $method) { var_dump($method->name); } /* getProperties()参数列表 const integer ReflectionProperty::IS_STATIC = 1 ; const integer ReflectionProperty::IS_PUBLIC = 256 ; const integer ReflectionProperty::IS_PROTECTED = 512 ; const integer ReflectionProperty::IS_PRIVATE = 1024 ; getMethods()参数列表 const integer ReflectionMethod::IS_STATIC = 1 ; const integer ReflectionMethod::IS_PUBLIC = 256 ; const integer ReflectionMethod::IS_PROTECTED = 512 ; const integer ReflectionMethod::IS_PRIVATE = 1024 ; const integer ReflectionMethod::IS_ABSTRACT = 2 ; const integer ReflectionMethod::IS_FINAL = 4 ; 如果要同时获取public和private属性,可以写成:ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED */