PHP的反射机制提供了一套反射API,用来访问和使用类、方法、属性、参数和注释等,好比能够经过一个对象知道这个对象所属的类,这个类包含哪些方法,这些方法须要传入什么参数,每一个参数是什么类型等等,不用建立类的实例也能够访问类的成员和方法,就算类成员定义为
private
也能够在外部访问。
官方文档提供了诸如 ReflectionClass
、ReflectionMethod
、ReflectionObject
、ReflectionExtension
等反射类及相应的API,用得最多的是 ReflectionClass
。php
为了演示反射效果,首先建立一个类(假设定义了一个类 User
),并实例化。基于这个实例,反射类能够访问 User
中的属性和方法。数组
<?php /** * 用户相关类 */ class User { public $username; private $password; public function __construct($username, $password) { $this->username = $username; $this->password = $password; } /** * 获取用户名 * @return string */ public function getUsername() { return $this->username; } /** * 设置用户名 * @param string $username */ public function setUsername($username) { $this->username = $username; } /** * 获取密码 * @return string */ private function getPassword() { return $this->password; } /** * 设置密码 * @param string $password */ private function setPassowrd($password) { $this->password = $password; } }
建立反射类实例架构
$refClass = new ReflectionClass(new User('liulu', '123456')); // 也能够写成 $refClass = new ReflectionClass('User'); // 将类名User做为参数,创建User类的反射类
反射属性并发
$properties = $refClass->getProperties(); // 获取User类的全部属性,返回ReflectionProperty的数组 $property = $refClass->getProperty('password'); // 获取User类的password属性 //$properties 结果以下: Array ( [0] => ReflectionProperty Object ( [name] => username [class] => User ) [1] => ReflectionProperty Object ( [name] => password [class] => User ) ) //$property 结果以下: ReflectionProperty Object ( [name] => password [class] => User )
反射方法框架
$methods = $refClass->getMethods(); // 获取User类的全部方法,返回ReflectionMethod数组 $method = $refClass->getMethod('getUsername'); // 获取User类的getUsername方法 //$methods 结果以下: Array ( [0] => ReflectionMethod Object ( [name] => __construct [class] => User ) [1] => ReflectionMethod Object ( [name] => getUsername [class] => User ) [2] => ReflectionMethod Object ( [name] => setUsername [class] => User ) [3] => ReflectionMethod Object ( [name] => getPassword [class] => User ) [4] => ReflectionMethod Object ( [name] => setPassowrd [class] => User ) ) //$method 结果以下: ReflectionMethod Object ( [name] => getUsername [class] => User )
反射注释分布式
$classComment = $refClass->getDocComment(); // 获取User类的注释文档,即定义在类以前的注释 $methodComment = $refClass->getMethod('setPassowrd')->getDocComment(); // 获取User类中setPassowrd方法的注释 //$classComment 结果以下: /** * 用户相关类 */ //$methodComment 结果以下: /** * 设置密码 * @param string $password */
反射实例化函数
$instance = $refClass->newInstance('admin', 123, '***'); // 从指定的参数建立一个新的类实例 //$instance 结果以下: User Object ( [username] => admin [password:User:private] => 123 ) 注:虽然构造函数中是两个参数,可是newInstance方法接受可变数目的参数,用于传递到类的构造函数。 $params = ['xiaoming', 'asdfg']; $instance = $refClass->newInstanceArgs($params); // 从给出的参数建立一个新的类实例 //$instance 结果以下: User Object ( [username] => xiaoming [password:User:private] => asdfg )
访问、执行类的公有方法——public微服务
$instance->setUsername('admin_1'); // 调用User类的实例调用setUsername方法设置用户名 $username = $instance->getUsername(); // 用过User类的实例调用getUsername方法获取用户名 echo $username . "\n"; // 输出 admin_1 // 也能够写成 $refClass->getProperty('username')->setValue($instance, 'admin_2'); // 经过反射类ReflectionProperty设置指定实例的username属性值 $username = $refClass->getProperty('username')->getValue($instance); // 经过反射类ReflectionProperty获取username的属性值 echo $username . "\n"; // 输出 admin_2 // 还能够写成 $refClass->getMethod('setUsername')->invoke($instance, 'admin_3'); // 经过反射类ReflectionMethod调用指定实例的方法,而且传送参数 $value = $refClass->getMethod('getUsername')->invoke($instance); // 经过反射类ReflectionMethod调用指定实例的方法 echo $value . "\n"; // 输出 admin_3
访问、执行类的非公有方法——private、protected高并发
try { // 正确写法 $property = $refClass->getProperty('password'); // ReflectionProperty Object ( [name] => password [class] => User ) $property->setAccessible(true); // 修改 $property 对象的可访问性 $property->setValue($instance, '987654321'); // 能够执行 $value = $property->getValue($instance); // 能够执行 echo $value . "\n"; // 输出 987654321 // 错误写法 $refClass->getProperty('password')->setAccessible(true); // 临时修改ReflectionProperty对象的可访问性 $refClass->getProperty('password')->setValue($instance, 'password'); // 不能执行,抛出不能访问异常 $refClass = $refClass->getProperty('password')->getValue($instance); // 不能执行,抛出不能访问异常 $refClass = $instance->password; // 不能执行,类自己的属性没有被修改,仍然是private } catch (Exception $e){ echo $e; } // 错误写法 结果以下: ReflectionException: Cannot access non-public member User::password in xxx.php
小结
- 无论反射类中定义的属性、方法是否为
public
,均可以获取到。 - 直接访问
protected
或则private
的属性、方法,会抛出异常。 - 访问非公有成员须要调用指定的
ReflectionProperty
或ReflectionMethod
对象setAccessible(true)
方法。
以上内容但愿帮助到你们,更多PHP大厂PDF,PHP进阶架构视频资料,PHP精彩好文能够关注公众号:PHP开源社区 或者访问:this