首页 » Web前端 » phprequire应用技巧_php反射运用技巧详解

phprequire应用技巧_php反射运用技巧详解

访客 2024-12-02 0

扫一扫用手机浏览

文章目录 [+]

1.反射有什么浸染

反射可以用作文档天生。
反射可以做hook插件功能或者动态代理

phprequire应用技巧_php反射运用技巧详解

通过反射我们可以得到一个类的干系属性:

phprequire应用技巧_php反射运用技巧详解
(图片来自网络侵删)

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

2.如何利用反射

2.1得到反射

如下有一个大略的Person类

<!--?php

class Person {

protected $name ;

protected $age;

public function getName() {

return $this--->name;

}

public static function getAge() {

return $this->age;

}

}

我们只须要把类名’Person’传给ReflectionClass即可得到Person的反射。

$class = new ReflectionClass('Person');

$instance = $class->newInstanceArgs();

2.2获取属性

$properties = $class->getProperties();

foreach( $properties as $pro ) {

echo $pro->getName().\"大众\n\公众;

}

输出如下:

2.3获取方法

$method = $class->getMethods();

var_dump($method);

输出如下:

2.4实行类的方法

$getName = $method[0];

$getName->invoke($instance);

//或者

$instance->getName();

上述就可以实行类中的方法。

标签:

相关文章

房山第一探寻历史文化名区的魅力与发展

房山区,位于北京市西南部,历史悠久,文化底蕴深厚。作为北京市的一个重要组成部分,房山区的发展始终与首都的发展紧密相连。房山区积极推...

Web前端 2025-02-18 阅读1 评论0

手机话费开钻代码数字时代的便捷生活

我们的生活越来越离不开手机。手机话费作为手机使用过程中的重要组成部分,其充值方式也在不断创新。手机话费开钻代码应运而生,为用户提供...

Web前端 2025-02-18 阅读1 评论0

探寻专业奥秘如何查询自己专业的代码

计算机科学已成为当今社会不可或缺的一部分。掌握一门专业代码对于个人发展具有重要意义。面对繁杂的学科体系,如何查询自己专业的代码成为...

Web前端 2025-02-18 阅读1 评论0