Reading from specific targets | 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)  Usage  Reading from specific targets

 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)

 Reading from specific targets
=============================

###  On this page

1. [ Methods ](#content-methods)
2. [ Properties ](#content-properties)
3. [ Constants ](#content-constants)
4. [ Parameters ](#content-parameters)
5. [ Functions ](#content-functions)
6. [ Repeated attributes ](#content-repeated-attributes)
7. [ Getting all attributes without filtering ](#content-getting-all-attributes-without-filtering)
8. [ Missing targets ](#content-missing-targets)

Beyond class-level attributes, you can read attributes from methods, properties, constants, parameters, and standalone functions.

All examples on this page use the following setup:

```
use Spatie\Attributes\Attributes;

#[Attribute(Attribute::TARGET_METHOD)]
class Route
{
    public function __construct(public string $path) {}
}

#[Attribute(Attribute::TARGET_PROPERTY)]
class Column
{
    public function __construct(public string $name) {}
}

#[Attribute(Attribute::TARGET_CLASS_CONSTANT)]
class Label
{
    public function __construct(public string $text) {}
}

#[Attribute(Attribute::TARGET_PARAMETER)]
class FromQuery
{
    public function __construct(public string $key = '') {}
}

#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
class Middleware
{
    public function __construct(public string $name) {}
}

class UserController
{
    #[Label('Active')]
    public const STATUS_ACTIVE = 'active';

    #[Column('email_address')]
    public string $email;

    #[Route('/users')]
    #[Middleware('auth')]
    #[Middleware('verified')]
    public function index(#[FromQuery('q')] string $query) {}
}
```

Methods
-----------------------------------------------------------------------------

```
$route = Attributes::onMethod(UserController::class, 'index', Route::class);

$route->path; // '/users'
```

Properties
--------------------------------------------------------------------------------------

```
$column = Attributes::onProperty(UserController::class, 'email', Column::class);

$column->name; // 'email_address'
```

Constants
-----------------------------------------------------------------------------------

```
$label = Attributes::onConstant(UserController::class, 'STATUS_ACTIVE', Label::class);

$label->text; // 'Active'
```

Parameters
--------------------------------------------------------------------------------------

Specify both the method name and parameter name:

```
$fromQuery = Attributes::onParameter(UserController::class, 'index', 'query', FromQuery::class);

$fromQuery->key; // 'q'
```

Functions
-----------------------------------------------------------------------------------

For standalone functions, use `onFunction()` with the fully qualified function name. For namespaced functions, you must use the full namespace:

```
#[Attribute]
class Deprecated
{
    public function __construct(public string $reason = '') {}
}

#[Deprecated('Use newHelper() instead')]
function oldHelper() {}

$deprecated = Attributes::onFunction('oldHelper', Deprecated::class);

$deprecated->reason; // 'Use newHelper() instead'

// For namespaced functions, use the fully qualified name:
// Attributes::onFunction('App\Helpers\oldHelper', Deprecated::class);
```

If the function does not exist, `onFunction()` returns `null`.

Repeated attributes
-----------------------------------------------------------------------------------------------------------------

When an attribute is repeatable, use the `getAllOn*` methods to retrieve all instances:

```
$middlewares = Attributes::getAllOnMethod(UserController::class, 'index', Middleware::class);

$middlewares[0]->name; // 'auth'
$middlewares[1]->name; // 'verified'
```

The same pattern is available for all target types:

```
Attributes::getAllOnMethod($class, $method, $attribute);                // array
Attributes::getAllOnProperty($class, $property, $attribute);            // array
Attributes::getAllOnConstant($class, $constant, $attribute);            // array
Attributes::getAllOnParameter($class, $method, $parameter, $attribute); // array
Attributes::getAllOnFunction($function, $attribute);                    // array
```

Getting all attributes without filtering
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

All `on*` and `getAllOn*` methods accept an optional attribute parameter. When omitted, they return all attributes on that target regardless of type:

```
// Get the first attribute on a method (any type)
$attribute = Attributes::onMethod(UserController::class, 'index');

// Get all attributes on a property
$attributes = Attributes::getAllOnProperty(UserController::class, 'email');

// Works on all targets
Attributes::onProperty($class, $property);
Attributes::onConstant($class, $constant);
Attributes::onParameter($class, $method, $parameter);
Attributes::onFunction($function);

Attributes::getAllOnMethod($class, $method);
Attributes::getAllOnProperty($class, $property);
Attributes::getAllOnConstant($class, $constant);
Attributes::getAllOnParameter($class, $method, $parameter);
Attributes::getAllOnFunction($function);
```

Missing targets
-----------------------------------------------------------------------------------------------------

The `on*` methods return `null` when the target doesn't exist or lacks the attribute. The `getAllOn*` methods return an empty array. No exceptions are thrown.

```
Attributes::onMethod(UserController::class, 'nonExistent', Route::class); // null
Attributes::getAllOnMethod(UserController::class, 'nonExistent', Middleware::class); // []
```
