PHP Reflection反射机制基础与Person类示例
自PHP5版本起,系统引入了Reflection这一新特性。借助此功能,开发人员能够对类(class)、接口(interface)、函数(function)、方法(method)以及扩展(extension)进行逆向工程分析。利用PHP代码即可全面获取任意对象的相关信息,并与其进行交互操作。 以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类的以下关键信息: 1. 常量(Constants) 2. 属性名称(Property Names) 3. 方法名称(Method Names) 4. 静态属性(Static Properties) 5. 命名空间(Namespace) 6. 判断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_PROTECTED。 这种用法对于熟悉PHP的开发者来说应该并不陌生。
通过调用$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
这确实令人惊讶,连注释内容都能被成功提取。 关于方法的获取:通过调用getMethods()可以获取类中所有的methods,该方法返回的是ReflectionMethod对象的数组。此处不再赘述具体遍历过程。 最后,可以通过ReflectionMethod来动态调用类内部的方法。
复制代码 代码如下: $data = array("id" => 1, "name" => "Chris", "biography" => "I am am a PHP developer"); foreach($data as $key => $value) { if(!$class->hasProperty($key)) { throw new Exception($key." is not a valid property"); } if(!$class->hasMethod("get".ucfirst($key))) { throw new Exception($key." is missing a getter"); } if(!$class->hasMethod("set".ucfirst($key))) { throw new Exception($key." is missing a setter"); } // Make a new object to interact with $object = new Person(); // Get the getter method and invoke it with the value in our data array $setter = $class->getMethod("set".ucfirst($key)); $ok = $setter->invoke($object, $value); // Get the setter method and invoke it $setter = $class->getMethod("get".ucfirst($key)); $objValue = $setter->invoke($object); // Now compare if($value == $objValue) { echo "Getter or Setter has modified the data.\n"; } else { echo "Getter and Setter does not modify the data.\n"; } }
这样的操作方式是否显得颇为有趣?