PHP的Reflection反射机制

原文地址: http://www.nowamagic.net/php/php_Reflection.php

PHP5添加了一项新的功能:Reflection。这个功能使得程序员可以

reverse-engineer[逆向工程] class, interface,function,method and extension[扩展库支持]。

通过PHP代码,就可以得到某object的所有信息,并且可以和它交互。

如假设以下Person类:

 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类的以下信息:

  • 常量 Contants
  • 属性 Property Names
  • 方法 Method Names
  • 静态属性 Static Properties
  • 命名空间 Namespace
  • Person类是否为final或者abstract

只要把类名"Person"传递给ReflectionClass就可以了:

 $class = new ReflectionClass('Person');

* 获取属性(Properties):

 $properties = $class->getProperties();
foreach($properties as $property) {
echo $property->getName()."\n";
}
// 输出:
// _allowDynamicAttributes
// id
// name
// biography

默认情况下,ReflectionClass会获取到所有的属性,private 和 protected的也可以。如果只想获取到private属性,就要额外传个参数:

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

可用参数列表:

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

如果要同时获取public 和private 属性,就这样写:ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PRIVATE

通过$property->getName()可以得到属性名,通过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

有点不可思议了吧。竟然连注释都可以取到。

* 获取方法(methods):通过getMethods() 来获取到类的所有methods。返回的是ReflectionMethod对象的数组。

不再演示。

赋一个利用反射加载类实例的工具类写法:

<?php
/**
* Desc: 反射加载类
* Class: ClassLoader
* Package: App\Lib
* User: zb
* Date: 2019/5/22 15:21
*/ namespace App\Lib; class ClassLoader
{
private static $obj = []; /**
* 上传类map
*/
private static function uploadClassMap ()
{
$map = [
'image' => 'App\Lib\Upload\Image',//key为传递参数key
'video' => 'App\Lib\Upload\Video',
];
return $map;
} /**
* model类map
*/
private static function modelClassMap ()
{
$map = [
'video' => 'App\Model\Video',
];
return $map;
} /**
* aliyun类map
*/
private static function aliyunClassMap ()
{
$map = [
'vod' => 'App\Lib\Aliyun\AliyunVod',
];
return $map;
} /**
* cache类map
*/
private static function cacheClassMap ()
{
$map = [
'video' => 'App\Lib\Cache\Video',
];
return $map;
} /**
* 类实例map指南
* @param string $guideKey 指南key
* @param string $classKey classKey
* @return array|bool
*/
private static function guideClassMap (string $guideKey, string $classKey)
{
$guideKey = strtolower($guideKey);
$guideMap = [
'upload' => self::uploadClassMap(),
'model' => self::modelClassMap(),
'aliyun' => self::aliyunClassMap(),
'cache' => self::cacheClassMap(),
];
return isset($guideMap[$guideKey][$classKey]) ? $guideMap[$guideKey][$classKey] : array();
} /**
* @param string $guideKey guideKey
* @param string $classKey classKey键
* @param array $params 构造函数参数
* @param bool $instance 是否需要实例化
* @return object|string 返回obj或字符串
* @throws \ReflectionException
*/
public static function initClass (string $guideKey, string $classKey, $params = [], $instance = true)
{
if (!$params && isset(self::$obj[$guideKey . '-' . $classKey]) && is_object(self::$obj[$guideKey . '-' . $classKey])) {
return self::$obj[$guideKey . '-' . $classKey];
} else {
unset(self::$obj[$guideKey . '-' . $classKey]);
} if (!self::guideClassMap($guideKey, $classKey)) {
throw new \ReflectionException('加载类不存在');
}
$class = self::guideClassMap($guideKey, $classKey);
try {
$obj = $instance ? (new \ReflectionClass($class))->newInstanceArgs($params) : $class;
if (!$params) {
self::$obj[$guideKey . '-' . $classKey] = $obj;
}
return $obj;
} catch (\ReflectionException $e) {
throw new \ReflectionException('类加载失败');
}
} }
上一篇:jquery跨域请求数据


下一篇:基础知识(javaWeb工程目录结构)及各文件夹的作用