PHP5 具备完整的反射API,添加对类、接口、函数、方法和扩展进行反向工程的能力。php
反射(Reflection)是指在PHP运行状态中,扩展分析PHP程序,导出或提取出关于
类
、方法
、属性
、参数
等的详细信息,包括注释。这种动态获取的信息以及动态调用对象的方法的功能称为反射API。反射是操纵面向对象范型中元模型的API,其功能十分强大,可帮助咱们构建复杂,可扩展的应用。sql
其用途如:自动加载插件,自动生成文档,甚至可用来扩充PHP语言。api
PHP反射api由若干类组成,可帮助咱们用来访问程序的元数据或者同相关的注释交互。借助反射咱们能够获取诸如类实现了那些方法,建立一个类的实例(不一样于用new建立),调用一个方法(也不一样于常规调用),传递参数,动态调用类的静态方法。数组
反射api是PHP内建的OOP技术扩展,包括一些类,异常和接口,综合使用他们可用来帮助咱们分析其它类,接口,方法,属性,方法和扩展。这些OOP扩展被称为反射。函数
日常咱们用的比较多的是 ReflectionClass
类和ReflectionMethod
类,例如:fetch
<?php class Person { /** * For the sake of demonstration, we"re setting this private */ private $_allowDynamicAttributes = false; /** * type=primary_autoincrement */ protected $id = 0; /** * type=varchar length=255 null */ protected $name; /** * type=text null */ protected $biography; public function getId() { return $this->id; } public function setId($v) { $this->id = $v; } public function getName() { return $this->name; } public function setName($v) { $this->name = $v; } public function getBiography() { return $this->biography; } public function setBiography($v) { $this->biography = $v; } }
经过ReflectionClass
,咱们能够获得Person
类的如下信息:ui
常量 Contants 属性 Property Names 方法 Method Names静态 属性 Static Properties 命名空间 Namespace Person类是否为final或者abstract Person类是否有某个方法
接下来反射它,只要把类名Person
传递给ReflectionClass
就能够了:this
$class = new ReflectionClass('Person'); // 创建 Person这个类的反射类 $instance = $class->newInstanceArgs($args); // 至关于实例化Person类
1)获取属性(Properties):spa
$properties = $class->getProperties(); foreach ($properties as $property) { echo $property->getName() . "\n"; } // 输出: // _allowDynamicAttributes // id // name // biography
默认状况下,ReflectionClass
会获取到全部的属性,private 和 protected的也能够。若是只想获取到private属性,就要额外传个参数:.net
$private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);
可用参数列表:
ReflectionProperty::IS_STATIC ReflectionProperty::IS_PUBLIC ReflectionProperty::IS_PROTECTED ReflectionProperty::IS_PRIVATE
经过$property->getName()
能够获得属性名。
2)获取注释:
经过getDocComment能够获得写给property的注释。
foreach ($properties as $property) { if ($property->isProtected()) { $docblock = $property->getDocComment(); preg_match('/ type\=([a-z_]*) /', $property->getDocComment(), $matches); echo $matches[1] . "\n"; } } // Output: // primary_autoincrement // varchar // text
3)获取类的方法
getMethods() 来获取到类的全部methods。 hasMethod(string) 是否存在某个方法 getMethod(string) 获取方法
4)执行类的方法:
$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'));
经过ReflectionMethod,咱们能够获得Person类的某个方法的信息:
是否public
、protected
、private
、static
类型
方法的参数列表
方法的参数个数
反调用类的方法
// 执行detail方法 $method = new ReflectionMethod('Person', 'test'); if ($method->isPublic() && !$method->isStatic()) { echo 'Action is right'; } echo $method->getNumberOfParameters(); // 参数个数 echo $method->getParameters(); // 参数对象数组
一、参数校验
function checkMethodParams($class, $method, $params){ $method = new ReflectionMethod(get_class($class), $method); $m_params = $method->getParameters(); $m_params_require_num = 0; foreach ($m_params as $param) { if (!$param->isOptional()) { $m_params_require_num += 1; } } if ($m_params_require_num > count($params)) { throw new HException('缺乏参数'); } }
二、获取一个类或扩展的内部详细信息
<?php $extension = 'pdo'; $e = new ReflectionExtension($extension); print "<?php\n\n// {$extension} Version: " . $e->getVersion() . "\n\n"; foreach ($e->getClasses() as $c) { print 'class ' . $c->name . " {\n"; foreach ($c->getMethods() as $m) { print ' '; if ($m->isPublic()) { print 'public'; } elseif ($m->isProtected()) { print 'protected'; } elseif ($m->isPrivate()) { print 'private'; } print ' function ' . $m->name . '('; $sep = ''; foreach ($m->getParameters() as $p) { print $sep; $sep = ', '; if ($p->isOptional()) print '$' . $p->name . ' = null' ; else print '$' . $p->name; } print "){}\n"; } print "}\n\n"; }
输出:
<?php // pdo Version: 1.0.4dev class PDOException { private function __clone(){} public function __construct($message = null, $code = null, $previous = null){} public function getMessage(){} public function getCode(){} public function getFile(){} public function getLine(){} public function getTrace(){} public function getPrevious(){} public function getTraceAsString(){} public function __toString(){} } class PDO { public function __construct($dsn, $username = null, $passwd = null, $options = null){} public function prepare($statement, $options = null){} public function beginTransaction(){} public function commit(){} public function rollBack(){} public function inTransaction(){} public function setAttribute($attribute, $value){} public function exec($query){} public function query(){} public function lastInsertId($seqname = null){} public function errorCode(){} public function errorInfo(){} public function getAttribute($attribute){} public function quote($string, $paramtype = null){} public function __wakeup(){} public function __sleep(){} public function getAvailableDrivers(){} } class PDOStatement { public function execute($bound_input_params = null){} public function fetch($how = null, $orientation = null, $offset = null){} public function bindParam($paramno, $param, $type = null, $maxlen = null, $driverdata = null){} public function bindColumn($column, $param, $type = null, $maxlen = null, $driverdata = null){} public function bindValue($paramno, $param, $type = null){} public function rowCount(){} public function fetchColumn($column_number = null){} public function fetchAll($how = null, $class_name = null, $ctor_args = null){} public function fetchObject($class_name = null, $ctor_args = null){} public function errorCode(){} public function errorInfo(){} public function setAttribute($attribute, $value){} public function getAttribute($attribute){} public function columnCount(){} public function getColumnMeta($column){} public function setFetchMode($mode, $params = null){} public function nextRowset(){} public function closeCursor(){} public function debugDumpParams(){} public function __wakeup(){} public function __sleep(){} } class PDORow { }
三、实现代理模式
<?php /** * Created by PhpStorm. * User: YJC * Date: 2016/6/9 009 * Time: 17:56 */ class Mysql{ public function query($sql){ echo $sql ."<br/>"; } } class Proxy{ private $obj; public function __construct($obj) { $this->obj = new $obj; } public function __call($name, $args){ $reflec = new ReflectionClass($this->obj); if($method = $reflec->getMethod($name)){ if($method->isPrivate()){ throw new Exception("$method 方法不能直接调用!"); } $this->selectDB($args); $this->beforeFilter($args); $method->invoke($this->obj, $args[0]); $this->afterFilter($args); } } public function selectDB($args){ $sql = $args[0]; $type = ''; if(stripos($sql, 'select') === 0){ $type = 'query'; echo '查询<br/>'; }elseif(stripos($sql, 'insert') === 0 || stripos($sql, 'update') === 0 || stripos($sql, 'delete') === 0){ $type = 'exec'; echo '更新<br/>'; } } public function beforeFilter($args){ echo '前置过滤<br/>'; } public function afterFilter($args){ echo '后置过滤<br/>'; } } //$obj = new Mysql(); $obj = new Proxy('Mysql'); $obj->query('insert * from user');