使用反射 API 在 PHP 8 中读取属性
在 PHP 8 中,我们使用类、属性、及类常量、方法、函数、参数来访问属性。
在 PHP 8 中,反射 API在每个匹配的反射对象上提供了 getAttribute() 方法。
getAttribute() 方法返回一个反射属性说明的数组,该数组可以被询问属性名称、参数并实例化表示属性的实例。
示例 − 在 PHP 8 中使用反射 API 读取属性
<?php
#[Reading]
#[Property(type: 'function', name: 'Student')]
function Student()
{
return "Student";
}
function getAttributes(Reflector $reflection)
{
$attributes = $reflection->getAttributes();
$finalresult = [];
foreach ($attributes as $attribute)
{
$finalresult[$attribute->getName() ] = $attribute->getArguments();
}
return $finalresult;
}
$reflection = new ReflectionFunction("Student");
print_r(getAttributes($reflection));
?>输出
Array ( [Reading] => Array ( ) [Property] => Array ( [type] => function [name] => Student ) )
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP