Reading class attributes | 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 class attributes

 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 class attributes
========================

###  On this page

1. [ Getting an attribute ](#content-getting-an-attribute)
2. [ Checking for an attribute ](#content-checking-for-an-attribute)
3. [ Repeated attributes ](#content-repeated-attributes)
4. [ Without an attribute filter ](#content-without-an-attribute-filter)
5. [ Using object instances ](#content-using-object-instances)

All examples on this page use the following attribute and class:

```
#[Attribute]
class Description
{
    public function __construct(
        public string $text,
    ) {}
}

#[Description('A user account')]
class User {}
```

Getting an attribute
--------------------------------------------------------------------------------------------------------------------

Use `get()` to retrieve a single attribute instance from a class. Returns `null` if the attribute is not present.

```
use Spatie\Attributes\Attributes;

$description = Attributes::get(User::class, Description::class);

$description->text; // 'A user account'
```

Checking for an attribute
-----------------------------------------------------------------------------------------------------------------------------------

Use `has()` to check if a class has a specific attribute:

```
Attributes::has(User::class, Description::class); // true
Attributes::has(User::class, SomeOtherAttribute::class); // false
```

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

If an attribute is marked as `IS_REPEATABLE`, use `getAll()` to retrieve all instances. Returns an empty array when no matching attributes exist.

```
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class Tag
{
    public function __construct(public string $name) {}
}

#[Tag('featured')]
#[Tag('popular')]
class Post {}

$tags = Attributes::getAll(Post::class, Tag::class);

$tags[0]->name; // 'featured'
$tags[1]->name; // 'popular'
```

Without an attribute filter
-----------------------------------------------------------------------------------------------------------------------------------------

All class-level methods also accept an optional attribute parameter. When omitted, they work with any attribute:

```
Attributes::get(User::class);    // first attribute, regardless of type
Attributes::has(User::class);    // true if the class has any attribute
Attributes::getAll(User::class); // all attributes on the class
```

Using object instances
--------------------------------------------------------------------------------------------------------------------------

All methods accept either a class string or an object instance:

```
$user = new User();

Attributes::get($user, Description::class);
Attributes::has($user, Description::class);
```
