承接 dentelis/php7-attribute-reader 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dentelis/php7-attribute-reader

Composer 安装命令:

composer require dentelis/php7-attribute-reader

包简介

Library for reading php8 attributes from legacy php7 code

README 文档

README

A library that brings PHP 8 attribute syntax to PHP 7.2+. The API mirrors PHP 8's native ReflectionAttribute as closely as possible, making future migration trivial.

PHP Version License

Installation

composer require dentelis/php7-attribute-reader

Quick Start

use AttributeReader\ReflectionMethodWithAttributes;

class UserController
{
    #[Route('/api/users', 'GET')]
    #[Auth('admin')]
    public function getUsers() {}
}

$method = new ReflectionMethodWithAttributes(UserController::class, 'getUsers');
$attributes = $method->getAttributes();

foreach ($attributes as $attr) {
    echo $attr->getName();          // FQCN, e.g. "App\Attributes\Route"
    print_r($attr->getArguments()); // ['/api/users', 'GET']
    $instance = $attr->newInstance(); // Route object
}

Comparison with PHP 8

// PHP 8 native:
$method = new ReflectionMethod(UserController::class, 'getUsers');
$attrs = $method->getAttributes(Route::class);
$route = $attrs[0]->newInstance();

// This library (PHP 7.2+):
$method = new ReflectionMethodWithAttributes(UserController::class, 'getUsers');
$attrs = $method->getAttributes(Route::class);
$route = $attrs[0]->newInstance();

Migration to PHP 8: replace new ReflectionMethodWithAttributes(...) with new ReflectionMethod(...).

Reflection Classes

The library provides drop-in wrappers for all four PHP reflection types. Each wrapper exposes getAttributes() with the same signature as PHP 8, and delegates all other method calls to the underlying reflection object.

ReflectionMethodWithAttributes

use AttributeReader\ReflectionMethodWithAttributes;

$method = new ReflectionMethodWithAttributes(UserController::class, 'getUsers');

// All attributes
$attrs = $method->getAttributes();

// Filter by exact class
$attrs = $method->getAttributes(Route::class);

// Filter by parent class (includes subclasses)
$attrs = $method->getAttributes(BaseAttribute::class, Attribute::IS_INSTANCEOF);

// Delegates to ReflectionMethod
echo $method->getName();
echo $method->getDeclaringClass()->getName();

ReflectionClassWithAttributes

use AttributeReader\ReflectionClassWithAttributes;

#[Controller('/api')]
#[Auth('admin')]
class UserController {}

$class = new ReflectionClassWithAttributes(UserController::class);
// or pass an object:
$class = new ReflectionClassWithAttributes(new UserController());

$attrs = $class->getAttributes();
$attrs = $class->getAttributes(Controller::class);

// Delegates to ReflectionClass
echo $class->getName();
$class->getMethods();

ReflectionPropertyWithAttributes

use AttributeReader\ReflectionPropertyWithAttributes;

class User {
    #[Column('email', unique: true)]
    public $email;
}

$prop = new ReflectionPropertyWithAttributes(User::class, 'email');
$attrs = $prop->getAttributes();
$attrs = $prop->getAttributes(Column::class);

// Delegates to ReflectionProperty
echo $prop->getName();
$prop->isPublic();

ReflectionFunctionWithAttributes

use AttributeReader\ReflectionFunctionWithAttributes;

#[Route('/handler')]
function myHandler() {}

$func = new ReflectionFunctionWithAttributes('myHandler');
$attrs = $func->getAttributes();
$attrs = $func->getAttributes(Route::class);

// Delegates to ReflectionFunction
echo $func->getName();

getAttributes() Signature

All four wrapper classes share the same getAttributes() signature:

getAttributes(?string $name = null, int $flags = 0): Attribute[]

Parameters:

  • $name — filter by FQCN (exact match by default)
  • $flags0 (default) or Attribute::IS_INSTANCEOF (includes subclasses)

Attribute — Returned Object

$attr->getName(): string;        // Fully qualified class name
$attr->getArguments(): array;    // Parsed arguments (positional and/or named)
$attr->newInstance(): object;    // Instantiates the attribute class
$attr->isRepeated(): bool;       // True if the same attribute appears more than once

Constants:

  • Attribute::IS_INSTANCEOF = 2 — matches ReflectionAttribute::IS_INSTANCEOF

Features

Filtering

$method = new ReflectionMethodWithAttributes(Controller::class, 'index');

// All attributes
$all = $method->getAttributes();

// By exact class
$routes = $method->getAttributes(Route::class);

// By parent class (includes subclasses)
$all = $method->getAttributes(BaseAttribute::class, Attribute::IS_INSTANCEOF);

Named Arguments

#[Route(path: '/users', method: 'POST')]
#[Cache(ttl: 3600, enabled: true)]
#[Route('/api/users', method: 'POST')]  // mixed positional and named

Multiple Attributes

// Separate lines
#[Route('/admin')]
#[Auth('admin')]
public function dashboard() {}

// Comma-separated
#[Route('/admin'), Auth('admin')]
public function dashboard() {}

Repeated Attributes

#[Middleware('auth')]
#[Middleware('logging')]
public function index() {}

$method = new ReflectionMethodWithAttributes(Controller::class, 'index');
$attrs = $method->getAttributes();
$attrs[0]->isRepeated(); // true

Expressions in Arguments

Arithmetic, bitwise, and string concatenation with correct operator precedence:

#[Cache(ttl: 60 * 60 * 24)]            // 86400
#[Cache(ttl: (2 + 3) * 10)]            // 50
#[Route(path: '/api' . '/users')]       // '/api/users'
#[Config(flags: 1 | 2 | 4)]            // 7
#[Cache(ttl: 2 ** 10)]                 // 1024

Supported operators: +, -, *, /, %, **, ., |, &, ^, ~, <<, >>

Constants in Arguments

Class constants, aliased imports, and global constants:

use App\Config\Limits;
use App\Config\Limits as L;

#[Cache(ttl: Limits::DEFAULT_TTL)]         // resolved via use statement
#[Cache(ttl: L::DEFAULT_TTL)]              // aliases work too
#[Cache(ttl: Limits::DEFAULT_TTL * 2)]     // expressions with constants
#[Cache(ttl: PHP_INT_SIZE)]                // global PHP constants
#[Config(name: Limits::APP_NAME . '-prod')]// concatenation with constants

Attribute Name Resolution

Attribute names are automatically resolved to FQCNs using the file's use statements and namespace — no explicit mapping required.

// In the source file:
use App\Attributes\Route;

#[Route('/users')]  // Resolved to "App\Attributes\Route"

Alternative: Static AttributeReader API

If you prefer not to use wrapper classes, AttributeReader exposes static methods that accept standard PHP reflection objects directly:

use AttributeReader\AttributeReader;

// Method attributes
AttributeReader::getMethodAttributes(ReflectionMethod $method, ?string $name = null, int $flags = 0): array;

// Class attributes
AttributeReader::getClassAttributes(ReflectionClass $class, ?string $name = null, int $flags = 0): array;

// Property attributes
AttributeReader::getPropertyAttributes(ReflectionProperty $property, ?string $name = null, int $flags = 0): array;

// Function attributes
AttributeReader::getFunctionAttributes(ReflectionFunction $function, ?string $name = null, int $flags = 0): array;

Example:

$method = new ReflectionMethod(UserController::class, 'getUsers');
$attrs = AttributeReader::getMethodAttributes($method, Route::class);
$route = $attrs[0]->newInstance();

Testing

composer test

Requirements

  • PHP 7.2 or higher

License

MIT

Credits

Developed by Dim Entelis

dentelis/php7-attribute-reader 适用场景与选型建议

dentelis/php7-attribute-reader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「annotations」 「reflection」 「attributes」 「legacy」 「compatibility」 「PHP7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dentelis/php7-attribute-reader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dentelis/php7-attribute-reader 我们能提供哪些服务?
定制开发 / 二次开发

基于 dentelis/php7-attribute-reader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 33
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-10