Introduction | php-attribute-reader | Spatie

 SPATIE

php-attribute-reader
====================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Php-attribute-reader](https://spatie.be/docs/php-attribute-reader/v1)  Introduction

 Version   v1

 Other versions for crawler [v1](https://spatie.be/docs/php-attribute-reader/v1)

- [ Introduction ](https://spatie.be/docs/php-attribute-reader/v1/introduction)
- [ Support us ](https://spatie.be/docs/php-attribute-reader/v1/support-us)
- [ Requirements ](https://spatie.be/docs/php-attribute-reader/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/php-attribute-reader/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/php-attribute-reader/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/php-attribute-reader/v1/changelog)
- [ About us ](https://spatie.be/docs/php-attribute-reader/v1/about-us)

Usage
-----

- [ Reading class attributes ](https://spatie.be/docs/php-attribute-reader/v1/usage/reading-class-attributes)
- [ Reading from specific targets ](https://spatie.be/docs/php-attribute-reader/v1/usage/reading-from-specific-targets)
- [ Discovering attributes ](https://spatie.be/docs/php-attribute-reader/v1/usage/discovering-attributes)
- [ Attribute inheritance ](https://spatie.be/docs/php-attribute-reader/v1/usage/attribute-inheritance)

  Php-attribute-reader
======================

 A clean API for working with PHP attributes

### Useful links

- [Repository](https://github.com/spatie/php-attribute-reader)
- [Discussions](https://github.com/spatie/php-attribute-reader/discussions)

Introduction
------------

###  On this page

1. [ We got badges ](#content-we-got-badges)

PHP 8.0 introduced attributes, a way to add structured metadata to classes, methods, properties, constants, parameters, and functions. The idea is great, but the reflection API to read them is verbose.

Imagine you have a controller with a `Route` attribute:

```
#[Route('/my-controller')]
class MyController
{
    // ...
}
```

Getting that attribute instance using native PHP requires this:

```
$reflection = new ReflectionClass(MyController::class);
$attributes = $reflection->getAttributes(Route::class, ReflectionAttribute::IS_INSTANCEOF);

if (count($attributes) > 0) {
    $route = $attributes[0]->newInstance();
}
```

With this package, it becomes a single call:

```
use Spatie\Attributes\Attributes;

$route = Attributes::get(MyController::class, Route::class);
```

The package handles instantiation, `IS_INSTANCEOF` matching, and returns `null` for missing targets instead of throwing exceptions.

It works on all attribute targets: classes, methods, properties, constants, parameters, and functions. It supports repeatable attributes and can discover all usages of an attribute across an entire class.

We got badges
-----------------------------------------------------------------------------------------------

[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/php-attribute-reader.svg?style=flat-square)](https://packagist.org/packages/spatie/php-attribute-reader)[![Tests](https://github.com/spatie/php-attribute-reader/actions/workflows/run-tests-pest.yml/badge.svg)](https://github.com/spatie/php-attribute-reader/actions/workflows/run-tests-pest.yml)
