推荐 PHP 属性(Attributes) 简洁读取 API 扩展包

推荐 PHP 属性(Attributes) 简洁读取 API 扩展包

PHP 8.0 引入的 Attributes(属性)为类、方法、属性、常量和参数添加结构化元数据提供了便利方式。尽管概念设计合理,但读取这些属性所需的反射 API 却显得过于冗长。原本简单的一行操作,往往要写成多行样板代码。若需在某个类中查找某属性的全部使用位置,还得编写层层嵌套的循环。

Spatie 近期发布的 php-attribute-reader 包提供了一套干净的静态 API,专门解决上述问题。

使用 Attribute Reader

假设有一个携带 Route 属性的控制器,目标是获取该属性的实例。使用原生 PHP 反射的写法如下:

php 复制代码
$reflection = new ReflectionClass(MyController::class);
$attributes = $reflection->getAttributes(Route::class, ReflectionAttribute::IS_INSTANCEOF);

$route = null;
if (count($attributes) > 0) {
    $route = $attributes[0]->newInstance();
}

这段代码长达五行,且仍需处理属性不存在的情况。使用 php-attribute-reader 后简化为:

php 复制代码
use Spatie\Attributes\Attributes;

$route = Attributes::get(MyController::class, Route::class);

单行完成。属性不存在时返回 null,无需额外的异常处理。

读取方法属性

从方法读取属性时,原生反射的繁琐程度进一步加剧。以下示例试图获取控制器 index 方法的 Route 属性:

php 复制代码
$reflection = new ReflectionMethod(MyController::class, 'index');
$attributes = $reflection->getAttributes(Route::class, ReflectionAttribute::IS_INSTANCEOF);

$route = null;
if (count($attributes) > 0) {
    $route = $attributes[0]->newInstance();
}

样板代码重复出现,仅反射类有所不同。该包通过专用方法统一处理各类目标:

php 复制代码
Attributes::onMethod(MyController::class, 'index', Route::class);
Attributes::onProperty(User::class, 'email', Column::class);
Attributes::onConstant(Status::class, 'ACTIVE', Label::class);
Attributes::onParameter(MyController::class, 'show', 'id', FromRoute::class);

全类扫描

原生反射在整类范围内查找属性时最为繁琐。假设某表单类的多个属性均携带 Validate 属性,原生 PHP 的实现大致如下:

php 复制代码
$results = [];
$class = new ReflectionClass(MyForm::class);

foreach ($class->getProperties() as $property) {
    foreach ($property->getAttributes(Validate::class, ReflectionAttribute::IS_INSTANCEOF) as $attr) {
        $results[] = ['attribute' => $attr->newInstance(), 'target' => $property];
    }
}

foreach ($class->getMethods() as $method) {
    foreach ($method->getAttributes(Validate::class, ReflectionAttribute::IS_INSTANCEOF) as $attr) {
        $results[] = ['attribute' => $attr->newInstance(), 'target' => $method];
    }
    foreach ($method->getParameters() as $parameter) {
        foreach ($parameter->getAttributes(Validate::class, ReflectionAttribute::IS_INSTANCEOF) as $attr) {
            $results[] = ['attribute' => $attr->newInstance(), 'target' => $parameter];
        }
    }
}

foreach ($class->getReflectionConstants() as $constant) {
    foreach ($constant->getAttributes(Validate::class, ReflectionAttribute::IS_INSTANCEOF) as $attr) {
        $results[] = ['attribute' => $attr->newInstance(), 'target' => $constant];
    }
}

对于常见的属性扫描需求,上述代码量显得过于庞大。使用该包后简化为:

php 复制代码
$results = Attributes::find(MyForm::class, Validate::class);

foreach ($results as $result) {
    $result->attribute; // 已实例化的属性对象
    $result->target;    // 反射对象
    $result->name;      // 例如 'email', 'handle.request'
}

所有返回的属性均为实例化对象,子类通过 IS_INSTANCEOF 自动匹配,目标不存在时返回 null 而非抛出异常。

实际应用

该包已被 Spatie 旗下的多个开源项目采用,包括 laravel-responsecachelaravel-event-sourcinglaravel-markdown,用于清理各代码库中积累属性读取相关的样板代码。

推荐 PHP 属性(Attributes) 简洁读取 API 扩展包

相关推荐
对象存储与RustFS1 分钟前
RustFS 后台扫描器与自愈调参:五档速度、位腐检测周期与并发上限
后端·rust·开源
天天喝旺仔11 分钟前
Docker 镜像瘦身实战:多阶段构建把体积缩小 90%
运维·后端·ci/cd·docker·云原生·容器·性能优化
Lost of 程序猿16 分钟前
ASP.NET Core Saga 分布式事务深度实战:备件采购跨服务长流程,如何保证“要么全成,要么全回“
分布式·后端·asp.net
卷无止境19 分钟前
Coding Agent 里的上下文 Compact,到底在压缩什么
后端·python
爱勇宝25 分钟前
公司没给活干,却因为员工看手机把人开了:法院判赔11万元
前端·后端·程序员
2601_9620710030 分钟前
【Java EE】SpringBoot的创建与简单使用
spring boot·后端·java-ee
Csvn32 分钟前
🐍 Day 7:文件 I/O 实战
后端
Supersist1 小时前
【Spring】 BeanFactory 和 ApplicationContext 体系结构
后端·spring
摇滚侠1 小时前
《SpringBoot 3:入门与应用实战》第 9 章 使用 WebMvc 开发进阶 阅读笔记 24
spring boot·笔记·后端
yunwei371 小时前
AgentCgroup:当 AI Agent 遇到操作系统资源
linux·人工智能·后端