reflection

PHP的反射ReflectionClass和ReflectMethod

ReflectClass

$class = new ReflectionClass('Person'); // 建立 Person这个类的反射类  
$args = $class->getProperties();
$instance = $class->newInstanceArgs($args); // 相当于实例化Person 类 

$class->getMethods();
$class->hasMethod(string);
$class->getMethod(string)

$private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);

ReflectionProperty::IS_STATIC
ReflectionProperty::IS_PUBLIC
ReflectionProperty::IS_PROTECTED
ReflectionProperty::IS_PRIVATE

//执行函数的三种方法
$instance->getName(); // 执行Person 里的方法getName
// 或者:
$method = $class->getMethod('getName'); // 获取Person 类中的getName方法
$method->invoke($instance);    // 执行getName 方法
// 或者:
$method = $class->getMethod('setName'); // 获取Person 类中的setName方法
$method->invokeArgs($instance, array('snsgou.com'));

ReflectMethod

// 执行detail方法
$method = new ReflectionMethod('Person', 'getName');
 
if ($method->isPublic() && !$method->isStatic()) {
 echo 'Action is right';
}
echo $method->getNumberOfParameters(); // 参数个数
echo $method->getParameters(); // 参数对象数组