damirco / attr
PHP Attributes Library Package
v0.1.1
2026-06-29 20:31 UTC
Requires
- php: >=8.1
Requires (Dev)
- phpunit/phpunit: ^10.0
README
A PHP library for checking if attributes are present on classes, properties, methods, parameters, functions, class constants, and enum cases.
Built on top of PHP 8+ Reflection API. Lightweight, zero dependencies.
Requirements
- PHP 8.1+
Installation
composer require damirco/attr
Quick Start
use Damirco\Attr\ClassAttribute; // 1. Define an attribute #[\Attribute] class MyAttribute {} // 2. Apply it to a class #[MyAttribute] class MyClass {} // 3. Check if it's present $attribute = new ClassAttribute(MyAttribute::class); $attribute->isPresentOn(MyClass::class); // true $attribute->isPresentOn(new MyClass()); // true (also works with instances)
Usage
Check class attributes
use Damirco\Attr\ClassAttribute; $attribute = new ClassAttribute(MyAttribute::class); $attribute->isPresentOn(MyClass::class); // by class name $attribute->isPresentOn(new MyClass()); // by instance
Check property attributes
use Damirco\Attr\PropertyAttribute; $attribute = new PropertyAttribute(MyAttribute::class); $attribute->isPresentOn(MyClass::class, 'propertyName');
Check method attributes
use Damirco\Attr\MethodAttribute; $attribute = new MethodAttribute(MyAttribute::class); $attribute->isPresentOn(MyClass::class, 'methodName');
Check parameter attributes
use Damirco\Attr\ParameterAttribute; $attribute = new ParameterAttribute(MyAttribute::class); $attribute->isPresentOn('myFunction', 'paramName');
Check function attributes
use Damirco\Attr\FunctionAttribute; $attribute = new FunctionAttribute(MyAttribute::class); $attribute->isPresentOn('myFunction');
Check class constant attributes
use Damirco\Attr\ClassConstantAttribute; $attribute = new ClassConstantAttribute(MyAttribute::class); $attribute->isPresentOn(MyClass::class, 'CONSTANT_NAME');
Check enum case attributes
use Damirco\Attr\EnumCaseAttribute; $attribute = new EnumCaseAttribute(MyAttribute::class); $attribute->isPresentOn(MyEnum::class, 'CASE_NAME');
How it works
Each class wraps PHP's ReflectionAttribute::getAttributes() method. The library:
- Creates a
Reflection*object for the target (class, property, method, etc.) - Calls
getAttributes($attributeClass, $flags)on it - Returns
trueif the result is not empty
The $flags parameter controls matching behavior:
\ReflectionAttribute::IS_INSTANCEOF(default) — matches if the attribute extends the given class0— exact class match only
// Example: MyAttribute extends BaseAttribute #[MyAttribute] class MyClass {} $attr = new ClassAttribute(BaseAttribute::class); $attr->isPresentOn(MyClass::class); // true (IS_INSTANCEOF by default) $attr = new ClassAttribute(BaseAttribute::class, 0); $attr->isPresentOn(MyClass::class); // false (exact match only)
Available classes
| Class | Checks | Target |
|---|---|---|
ClassAttribute |
Class-level attributes | Class name or instance |
PropertyAttribute |
Property attributes | Class + property name |
MethodAttribute |
Method attributes | Class + method name |
ParameterAttribute |
Parameter attributes | Function + parameter name |
FunctionAttribute |
Function attributes | Function name or Closure |
ClassConstantAttribute |
Class constant attributes | Class + constant name |
EnumCaseAttribute |
Enum case attributes | Enum + case name |
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) );
Fluent setters
$attribute = new ClassAttribute(MyAttribute::class) ->setAttributeClass(AnotherAttribute::class) ->setFlags(0);
Testing
composer test