damirco/attr
Composer 安装命令:
composer require damirco/attr
包简介
PHP Attributes Library Package
README 文档
README
PHP library for checking the presence of attributes on classes, properties, methods, parameters, functions, class constants, and enum cases.
Requirements
- PHP 8.0+
Installation
composer require damirco/attr
Usage
use Damirco\Attr\ClassAttribute; use Damirco\Attr\PropertyAttribute; use Damirco\Attr\MethodAttribute; use Damirco\Attr\ParameterAttribute; use Damirco\Attr\FunctionAttribute; use Damirco\Attr\ClassConstantAttribute; use Damirco\Attr\EnumCaseAttribute;
Check class attributes
$attribute = new ClassAttribute(MyAttribute::class); $attribute->isPresentOn(MyClass::class); // by class name $attribute->isPresentOn(new MyClass()); // by instance
Check property attributes
$attribute = new PropertyAttribute(MyAttribute::class); $attribute->isPresentOn(MyClass::class, 'propertyName');
Check method attributes
$attribute = new MethodAttribute(MyAttribute::class); $attribute->isPresentOn(MyClass::class, 'methodName');
Check parameter attributes
$attribute = new ParameterAttribute(MyAttribute::class); $attribute->isPresentOn(myFunction(...), 'paramName');
Check function attributes
$attribute = new FunctionAttribute(MyAttribute::class); $attribute->isPresentOn(myFunction(...));
Check class constant attributes
$attribute = new ClassConstantAttribute(MyAttribute::class); $attribute->isPresentOn(MyClass::class, 'CONSTANT_NAME');
Check enum case attributes
$attribute = new EnumCaseAttribute(MyAttribute::class); $attribute->isPresentOn(MyEnum::class, 'CASE_NAME');
Available classes
| Class | Checks |
|---|---|
ClassAttribute |
Class-level attributes |
PropertyAttribute |
Property attributes |
MethodAttribute |
Method attributes |
ParameterAttribute |
Function/method parameter attributes |
FunctionAttribute |
Function attributes |
ClassConstantAttribute |
Class constant attributes |
EnumCaseAttribute |
Enum case attributes |
Configuration
All classes extend AbstractAttribute and accept two constructor parameters:
new ClassAttribute( attribute_class: MyAttribute::class, // attribute to check for flags: \ReflectionAttribute::IS_INSTANCEOF // matching mode (default) );
Flags
\ReflectionAttribute::IS_INSTANCEOF(default) — matches if the attribute is an instance of the given class0— exact class match only
Fluent setters
$attribute = new ClassAttribute(MyAttribute::class) ->setAttributeClass(AnotherAttribute::class) ->setFlags(0);
Testing
composer test
License
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-28