PHPackages                             ock/reflector-aware-attributes - 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. ock/reflector-aware-attributes

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

ock/reflector-aware-attributes
==============================

1.x-dev(12mo ago)1164MITPHPPHP &gt;=8.3CI passing

Since May 23Pushed 11mo agoCompare

[ Source](https://github.com/ock-php/reflector-aware-attributes)[ Packagist](https://packagist.org/packages/ock/reflector-aware-attributes)[ RSS](/packages/ock-reflector-aware-attributes/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (4)Used By (4)

Reflector-aware attributes
==========================

[](#reflector-aware-attributes)

Provides mechanisms for attribute objects to know about the symbol they are attached to.

Motivation
----------

[](#motivation)

An attribute may want to know about the symbol it describes, to:

- Perform validation, and throw an exception if it is attached to the wrong symbol.
- Populate default values based on the symbol. E.g., an attribute attached to a parameter may populate a property value based on the parameter type.

Usage
-----

[](#usage)

### Using AttributeConstructor::getReflector().

[](#using-attributeconstructorgetreflector)

Create an attribute class that calls `AttributeConstructor::getReflector()` in the constructor.

```
use Ock\ReflectorAwareAttributes\AttributeConstructor;
use Ock\ReflectorAwareAttributes\ReflectorAwareAttributeInterface;

#[\Attribute(\Attribute::TARGET_ALL)]
class MyAttribute {

  public readonly \Reflector $reflector;

  public function __construct(): void {
    $this->reflector = AttributeConstructor::getReflector();
  }

}
```

Attach the attribute to a class or other symbol.

```
#[MyAttribute]
class MyClass {}
```

Call `get_attributes()` to extract attributes instances from the class.

```
use function Ock\ReflectorAwareAttributes\get_attributes;

$reflection_class = new \ReflectionClass(MyClass::class);
$attribute_instances = get_attributes($reflection_class, MyAttribute::class);

assert($attribute_instances[0] instanceof MyAttribute);
assert($attribute_instances[0]->reflector === $reflection_class);
```

### Using the interface with -&gt;setReflector().

[](#using-the-interface-with--setreflector)

Create an attribute class that implements `ReflectorAwareAttributeInterface`.

```
use Ock\ReflectorAwareAttributes\ReflectorAwareAttributeInterface;

#[\Attribute(\Attribute::TARGET_ALL)]
class MyReflectorAwareAttribute implements ReflectorAwareAttributeInterface {

  public readonly \Reflector $reflector;

  public function setReflector(\Reflector $reflector): void {
    $this->reflector = $reflector;
  }

}
```

Attach the attribute to a class or other symbol.

```
#[MyReflectorAwareAttribute]
function foo() {}
```

Call `get_attributes()` to extract attributes instances from the function.

```
use function Ock\ReflectorAwareAttributes\get_attributes;

$reflection_function = new \ReflectionFunction('foo');
$attribute_instances = get_attributes($reflection_function, MyReflectorAwareAttribute::class);
assert($attribute_instances[0] instanceof MyReflectorAwareAttribute);
assert($attribute_instances[0]->reflector === $reflection_function);
```

### Using AttributeReader.

[](#using-attributereader)

The AttributeReader class allows to support attribute types that do not use the mechanisms from this package.

Assume you have an attribute class like this, coming from a 3rd party package:

```
#[\Attribute(\Attribute::TARGET_CLASS)]
class ThirdPartyAttribute {
  public readonly \Reflector $reflector;
}
```

You can create a custom instantiator that will populate the property.

Ideally this should use the decorator pattern, so that multiple operations can be applied.

```
use Ock\ReflectorAwareAttributes\Instantiator\AttributeInstantiatorInterface;
use Ock\ReflectorAwareAttributes\Reader\AttributeReader;

class MyInstantiator implements AttributeInstantiatorInterface {

  public function __construct(
    private readonly AttributeInstantiatorInterface $decorated,
  ) {}

  public function newInstance(
    \ReflectionAttribute $attribute,
    \ReflectionClassConstant|\ReflectionParameter|\ReflectionClass|\ReflectionProperty|\ReflectionFunctionAbstract $reflector,
  ): object {
    $instance = $this->decorated->newInstance($attribute, $reflector);
    if ($instance instanceof ThirdPartyAttribute) {
      $instance->reflector = $reflector;
    }
    return $instance;
  }

}
```

Now we can use a reader with this instantiator decorator to get the attribute instances.

```
#[ThirdPartyAttribute]
class C {}

$reader = AttributeReader::basic()
  ->withDecoratingInstantiator(
    fn (AttributeInstantiatorInterface $decorated) => new MyInstantiator($decorated),
  );

$reflection_class = new \ReflectionClass(C::class);
$instances = $reader->getInstances($reflection_class, ThirdPartyAttribute::class);

assert($instances[0] instanceof ThirdPartyAttribute);
assert($instances[0]->reflector === $reflection_class);
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance50

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 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 ~0 days

Total

2

Last Release

360d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/150032?v=4)[Andreas Hennings](/maintainers/donquixote)[@donquixote](https://github.com/donquixote)

---

Top Contributors

[![donquixote](https://avatars.githubusercontent.com/u/150032?v=4)](https://github.com/donquixote "donquixote (8 commits)")

---

Tags

phpphp-attributesphp-reflection

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ock-reflector-aware-attributes/health.svg)

```
[![Health](https://phpackages.com/badges/ock-reflector-aware-attributes/health.svg)](https://phpackages.com/packages/ock-reflector-aware-attributes)
```

###  Alternatives

[bumbummen99/shoppingcart

Laravel Shoppingcart

518555.4k3](/packages/bumbummen99-shoppingcart)[apeisa/process-redirects

161.1k](/packages/apeisa-process-redirects)

PHPackages © 2026

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