PHPackages                             damirco/attr - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. damirco/attr

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

damirco/attr
============

PHP Attributes Library Package

v0.1.1(1mo ago)02↓50%MITPHPPHP &gt;=8.1

Since Jun 28Pushed 1mo agoCompare

[ Source](https://github.com/damirco/attr)[ Packagist](https://packagist.org/packages/damirco/attr)[ RSS](/packages/damirco-attr/feed)WikiDiscussions main Synced 2w ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

attr
====

[](#attr)

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
------------

[](#requirements)

- PHP 8.1+

Installation
------------

[](#installation)

```
composer require damirco/attr
```

Quick Start
-----------

[](#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
-----

[](#usage)

### Check class attributes

[](#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

[](#check-property-attributes)

```
use Damirco\Attr\PropertyAttribute;

$attribute = new PropertyAttribute(MyAttribute::class);

$attribute->isPresentOn(MyClass::class, 'propertyName');
```

### Check method attributes

[](#check-method-attributes)

```
use Damirco\Attr\MethodAttribute;

$attribute = new MethodAttribute(MyAttribute::class);

$attribute->isPresentOn(MyClass::class, 'methodName');
```

### Check parameter attributes

[](#check-parameter-attributes)

```
use Damirco\Attr\ParameterAttribute;

$attribute = new ParameterAttribute(MyAttribute::class);

$attribute->isPresentOn('myFunction', 'paramName');
```

### Check function attributes

[](#check-function-attributes)

```
use Damirco\Attr\FunctionAttribute;

$attribute = new FunctionAttribute(MyAttribute::class);

$attribute->isPresentOn('myFunction');
```

### Check class constant attributes

[](#check-class-constant-attributes)

```
use Damirco\Attr\ClassConstantAttribute;

$attribute = new ClassConstantAttribute(MyAttribute::class);

$attribute->isPresentOn(MyClass::class, 'CONSTANT_NAME');
```

### Check enum case attributes

[](#check-enum-case-attributes)

```
use Damirco\Attr\EnumCaseAttribute;

$attribute = new EnumCaseAttribute(MyAttribute::class);

$attribute->isPresentOn(MyEnum::class, 'CASE_NAME');
```

How it works
------------

[](#how-it-works)

Each class wraps PHP's `ReflectionAttribute::getAttributes()` method. The library:

1. Creates a `Reflection*` object for the target (class, property, method, etc.)
2. Calls `getAttributes($attributeClass, $flags)` on it
3. Returns `true` if the result is not empty

The `$flags` parameter controls matching behavior:

- `\ReflectionAttribute::IS_INSTANCEOF` (default) — matches if the attribute extends the given class
- `0` — 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
-----------------

[](#available-classes)

ClassChecksTarget`ClassAttribute`Class-level attributesClass name or instance`PropertyAttribute`Property attributesClass + property name`MethodAttribute`Method attributesClass + method name`ParameterAttribute`Parameter attributesFunction + parameter name`FunctionAttribute`Function attributesFunction name or Closure`ClassConstantAttribute`Class constant attributesClass + constant name`EnumCaseAttribute`Enum case attributesEnum + case nameConfiguration
-------------

[](#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

[](#fluent-setters)

```
$attribute = new ClassAttribute(MyAttribute::class)
    ->setAttributeClass(AnotherAttribute::class)
    ->setFlags(0);
```

Testing
-------

[](#testing)

```
composer test
```

License
-------

[](#license)

[MIT](LICENSE)

###  Health Score

34

—

LowBetter than 74% of packages

Maintenance91

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

2

Last Release

44d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8d466fa8fbb467505e0bf3d33385aa4a2babedff90691092ce3e6a764b808a9b?d=identicon)[damirco](/maintainers/damirco)

---

Top Contributors

[![damirco](https://avatars.githubusercontent.com/u/35264920?v=4)](https://github.com/damirco "damirco (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/damirco-attr/health.svg)

```
[![Health](https://phpackages.com/badges/damirco-attr/health.svg)](https://phpackages.com/packages/damirco-attr)
```

###  Alternatives

[harishdurga/laravel-quiz

Provides Quiz Functionality

1736.8k](/packages/harishdurga-laravel-quiz)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
