这篇文章主要介绍了关于PHP的Reflection反射机制的介绍,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
PHP5添加了一项新的功能:Reflection。这个功能使得程序员可以
reverse-engineer[逆向工程] class, interface,function,method and extension[扩展库支持]。
通过PHP代码,就可以得到某object的所有信息,并且可以和它交互。
如假设以下Person类:
1 class Person { 2 /** 3 * For the sake of demonstration, we"re setting this private 4 */ 5 private $_allowDynamicAttributes = false; 6 7 /** 8 * type=primary_autoincrement 9 */ 10 protected $id = 0; 11 12 /** 13 * type=varchar length=255 null 14 */ 15 protected $name; 16 17 /** 18 * type=text null19 */ 20 protected $biography; 21 public function getId() { 22 return $this->id; 23 } 24 public function setId($v) { 25 $this->id = $v; 26 } 27 public function getName() { 28 return $this->name; 29 } 30 public function setName($v) { 31 $this->name = $v; 32 } 33 public function getBiography() { 34 return $this->biography; 35 } 36 public function setBiography($v) { 37 $this->biography = $v; 38 } 39 }
通过ReflectionClass,我们可以得到Person类的以下信息:
常量 Contants
属性 Property Names
方法 Method Names
静态属性 Static Properties
命名空间 Namespace
Person类是否为final或者abstract
只要把类名"Person"传递给ReflectionClass就可以了:
1 $class = new ReflectionClass('Person');
* 获取属性(Properties):
1 $properties = $class->getProperties();2 foreach($properties as $property) { 3 echo $property->getName()."\n";4 } 5 // 输出:6 // _allowDynamicAttributes7 // id8 // name9 // biography
默认情况下,ReflectionClass会获取到所有的属性,private 和 protected的也可以。如果只想获取到private属性,就要额外传个参数:
1 $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
通过$property->getName()可以得到属性名,通过getDocComment可以得到写给property的注释。
1 foreach($properties as $property) { 2 if($property->isProtected()) { 3 $docblock = $property->getDocComment(); 4 preg_match('/ type\=([a-z_]*) /', $property->getDocComment(), $matches); 5 echo $matches[1]."\n"; 6 } 7 } 8 // Output: 9 // primary_autoincrement 10 // varchar 11 // text
有点不可思议了吧。竟然连注释都可以取到。
* 获取方法(methods):通过getMethods() 来获取到类的所有methods。返回的是ReflectionMethod对象的数组。
不再演示。
* 最后,通过ReflectionMethod来调用类里面的method。
$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"; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上是PHP的Reflection反射机制的介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

要保护应用免受与会话相关的XSS攻击,需采取以下措施:1.设置HttpOnly和Secure标志保护会话cookie。2.对所有用户输入进行输出编码。3.实施内容安全策略(CSP)限制脚本来源。通过这些策略,可以有效防护会话相关的XSS攻击,确保用户数据安全。

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显着提升应用在高并发环境下的效率。

thesession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceIsiseededeedeedeedeedeedeedto to to avoidperformance andununununununexpectedLogOgouts.3)

在PHP中,可以使用session_name()函数配置会话名称。具体步骤如下:1.使用session_name()函数设置会话名称,例如session_name("my_session")。2.在设置会话名称后,调用session_start()启动会话。配置会话名称可以避免多应用间的会话数据冲突,并增强安全性,但需注意会话名称的唯一性、安全性、长度和设置时机。

会话ID应在登录时、敏感操作前和每30分钟定期重新生成。1.登录时重新生成会话ID可防会话固定攻击。2.敏感操作前重新生成提高安全性。3.定期重新生成降低长期利用风险,但需权衡用户体验。

在PHP中设置会话cookie参数可以通过session_set_cookie_params()函数实现。1)使用该函数设置参数,如过期时间、路径、域名、安全标志等;2)调用session_start()使参数生效;3)根据需求动态调整参数,如用户登录状态;4)注意设置secure和httponly标志以提升安全性。

在PHP中使用会话的主要目的是维护用户在不同页面之间的状态。1)会话通过session_start()函数启动,创建唯一会话ID并存储在用户cookie中。2)会话数据保存在服务器上,允许在不同请求间传递数据,如登录状态和购物车内容。

如何在子域名间共享会话?通过设置通用域名的会话cookie实现。1.在服务器端设置会话cookie的域为.example.com。2.选择合适的会话存储方式,如内存、数据库或分布式缓存。3.通过cookie传递会话ID,服务器根据ID检索和更新会话数据。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

Dreamweaver CS6
视觉化网页开发工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!