PHPackages                             reedware/is-attribute - 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. reedware/is-attribute

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

reedware/is-attribute
=====================

Adds truth test helper for checking if class is an attribute.

v1.0.0(2y ago)07MITPHPPHP &gt;=8.0

Since Jun 25Pushed 2y ago1 watchersCompare

[ Source](https://github.com/tylernathanreed/is-attribute)[ Packagist](https://packagist.org/packages/reedware/is-attribute)[ RSS](/packages/reedware-is-attribute/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

Is Attribute
============

[](#is-attribute)

[![Automated Tests](https://github.com/tylernathanreed/is-attribute/actions/workflows/tests.yml/badge.svg)](https://github.com/tylernathanreed/is-attribute/actions/workflows/tests.yml)[![Coding Standards](https://github.com/tylernathanreed/is-attribute/actions/workflows/coding-standards.yml/badge.svg)](https://github.com/tylernathanreed/is-attribute/actions/workflows/coding-standards.yml)[![Code Coverage](https://github.com/tylernathanreed/is-attribute/actions/workflows/coverage.yml/badge.svg)](https://github.com/tylernathanreed/is-attribute/actions/workflows/coverage.yml)[![Static Analysis](https://github.com/tylernathanreed/is-attribute/actions/workflows/static-analysis.yml/badge.svg)](https://github.com/tylernathanreed/is-attribute/actions/workflows/static-analysis.yml)[![Latest Stable Version](https://camo.githubusercontent.com/86795d43faaf66ee7e74ca99cddedb7b4f62ceb883ee96a7cbe50122a47dab12/68747470733a2f2f706f7365722e707567782e6f72672f72656564776172652f69732d6174747269627574652f762f737461626c65)](https://packagist.org/packages/reedware/is-attribute)

This package adds a truth test helper for checking if class is an attribute.

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

[](#introduction)

[PHP Attributes](https://www.php.net/manual/en/language.attributes.overview.php) were introduced in PHP 8.0. From the documentation, you can see that Attributes are defined similar to classes:

```
#[Attribute]
class SetUp
{
    //
}
```

However, if you're handed the name of a class, there's no good way to know if that class is a PHP Attribute or not. This is where the `is_attribute()` method, as defined by this package, comes in.

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

[](#installation)

Install this package using Composer:

```
composer require reedware/is-attribute

```

Usage
-----

[](#usage)

```
function is_attribute(string|object|null $class, ?int $target = null, int $match = TARGET_MATCH_EQUALS): bool
```

### Class Argument

[](#class-argument)

Let's start with the basics. For more use-cases, you'll only need to pass one parameter to `is_attribute()`, being the class itself.

Here's an example of that:

```
echo is_attribute(SetUp::class); // true
echo is_attribute(CopyFile::class); // false
```

### Target Argument

[](#target-argument)

If you care what targets the attributes support, this is where an optional second parameter comes in.

For example, you can define an attribute like so:

```
#[Attribute(Attribute::TARGET_CLASS_CONSTANT|Attribute::TARGET_PROPERTY)]
class Serialize
{
    //
}
```

This defines a `Serialize` attribute that can applied to a class or property.

To check if an attribute can be applied to a class or property, you can pass in targets as the second parameter to `is_attribute`:

```
echo is_attribute(Serialize::class, Attribute::TARGET_CLASS_CONSTANT|Attribute::TARGET_PROPERTY); // true
```

Note that checking one or the other will return false:

```
echo is_attribute(Serialize::class, Attribute::TARGET_CLASS_CONSTANT); // false
echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY); // false
```

When the second argument is not specified (e.g. `null`), the specified class must simply be an Attribute, regardless of target. This is different from passing in `Attribute::TARGET_ALL` as the second argument, which will require the attribute to specify all targets (which is the default).

```
echo is_attribute(Serialize::class, Attribute::TARGET_ALL); // false
```

This is because the second parameter must be an *exact* match to the attribute.

### Match Argument

[](#match-argument)

If you don't want to do an exact match, you can use the optional third parameter.

#### TARGET\_MATCH\_EQUALS

[](#target_match_equals)

This is the default value of the third argument, and exhibits the behavior already described above.

#### TARGET\_MATCH\_INCLUDES

[](#target_match_includes)

This match setting requires ALL of the provided targets to be included (e.g. and/conjunction).

```
echo is_attribute(Serialize::class, Attribute::TARGET_CLASS_CONSTANT, TARGET_MATCH_INCLUDES); // true
echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY, TARGET_MATCH_INCLUDES); // true
echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER, TARGET_MATCH_INCLUDES); // false
```

#### TARGET\_MATCH\_ANY

[](#target_match_any)

This match setting requires ANY of the provided targets to be included (e.g. or/disjunction).

```
echo is_attribute(Serialize::class, Attribute::TARGET_CLASS_CONSTANT, TARGET_MATCH_INCLUDES); // true
echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY, TARGET_MATCH_INCLUDES); // true
echo is_attribute(Serialize::class, Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER, TARGET_MATCH_INCLUDES); // true
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

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

Unknown

Total

1

Last Release

1057d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6bfd8171901449cf1e05fa5db261a2f424abca5e26818ce54b95442de0661754?d=identicon)[tylernathanreed](/maintainers/tylernathanreed)

---

Top Contributors

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

---

Tags

phpattributesis-attribute

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/reedware-is-attribute/health.svg)

```
[![Health](https://phpackages.com/badges/reedware-is-attribute/health.svg)](https://phpackages.com/packages/reedware-is-attribute)
```

###  Alternatives

[psalm/attributes

A collection of PHP 8 Attributes that Psalm can understand

19120.0k2](/packages/psalm-attributes)[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21421.6k](/packages/imanghafoori-laravel-anypass)

PHPackages © 2026

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