PHPackages                             welcomattic/has-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. welcomattic/has-attributes

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

welcomattic/has-attributes
==========================

Check if a class, a method or a property has an attribute

v1.1.0(2y ago)122MITPHPPHP &gt;=8.2

Since Nov 20Pushed 1y ago1 watchersCompare

[ Source](https://github.com/welcoMattic/has-attribute)[ Packagist](https://packagist.org/packages/welcomattic/has-attributes)[ RSS](/packages/welcomattic-has-attributes/feed)WikiDiscussions main Synced 1mo ago

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

HasAttribute
============

[](#hasattribute)

For years, PHP has made it easy to check whether an object is an instance of a given class. However, it's not as simple to check that an object's class has Attribute.

This package provides some functions to make it easier to check whether an object's class has a given Attribute.

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

[](#installation)

```
$ composer require welcomattic/has-attribute
```

Usage
-----

[](#usage)

```
use Welcomattic\HasAttribute\HasAttributeMode;

#[\Attribute(\Attribute::TARGET_CLASS)]
class ClassAttribute {}

#[\Attribute(\Attribute::TARGET_CLASS)]
class SecondClassAttribute {}

#[\Attribute(\Attribute::TARGET_CLASS)]
class ThirdClassAttribute {}

#[ClassAttribute]
#[SecondClassAttribute]
class Foo {}

class_has_attribute(Foo::class, ClassAttribute::class); // true
class_has_attribute(Foo::class, ThirdClassAttribute::class); // false
class_has_attribute(Foo::class, [ClassAttribute::class, SecondClassAttribute::class]); // true
class_has_attribute(Foo::class, [ClassAttribute::class, SecondClassAttribute::class], HasAttributeMode::ATTRIBUTES_ALL_OF); // true (default mode)
class_has_attribute(Foo::class, [ClassAttribute::class, SecondClassAttribute::class], HasAttributeMode::ATTRIBUTES_ANY_OF); // true
class_has_attribute(Foo::class, [ClassAttribute::class, SecondClassAttribute::class], HasAttributeMode::ATTRIBUTES_ONE_OF); // false
```

You can also check on object methods or properties:

```
#[\Attribute(\Attribute::TARGET_METHOD)]
class MethodAttribute {}

#[\Attribute(\Attribute::TARGET_PROPERTY)]
class PropertyAttribute {}

class Foo {
    #[MethodAttribute]
    public function bar() {}

    #[PropertyAttribute]
    public $baz;
}

method_has_attribute(Foo::class, MethodAttribute::class, 'bar'); // true
property_has_attribute(Foo::class, PropertyAttribute::class, 'baz'); // true
```

It also supports Attribute inheritance:

```
#[\Attribute(\Attribute::TARGET_CLASS)]
class ClassAttributeInterface {}

#[\Attribute(\Attribute::TARGET_CLASS)]
class ChildClassAttribute extends ClassAttributeInterface {}

#[ChildClassAttribute]
class Foo {}

class_has_attribute(Baz::class, [ClassAttributeInterface::class]); // true
```

Credits
-------

[](#credits)

This package is based on an idea from [lyrixx](https://github.com/lyrixx), and was implemented by [welcomattic](https://github.com/welcomattic) and [Korbeil](https://github.com/Korbeil).

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

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

Total

2

Last Release

901d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c10405592336ec9bc0356b8c56ffa199a9eff54b744a079814f3b638a2701056?d=identicon)[welcomattic](/maintainers/welcomattic)

---

Top Contributors

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

---

Tags

utilityattribute

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/welcomattic-has-attributes/health.svg)

```
[![Health](https://phpackages.com/badges/welcomattic-has-attributes/health.svg)](https://phpackages.com/packages/welcomattic-has-attributes)
```

###  Alternatives

[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k394.3M1.5k](/packages/nette-utils)[danielstjules/stringy

A string manipulation library with multibyte support

2.4k26.0M191](/packages/danielstjules-stringy)[voku/arrayy

Array manipulation library for PHP, called Arrayy!

4875.5M16](/packages/voku-arrayy)[mage2tv/magento-cache-clean

This package has been migrated to mage-os/magento-cache-clean. Please switch over at your convenience.

5432.1M3](/packages/mage2tv-magento-cache-clean)[vaimo/composer-patches

Applies a patch from a local or remote file to any package that is part of a given composer project. Patches can be defined both on project and on package level. Optional support for patch versioning, sequencing, custom patch applier configuration and patch command for testing/troubleshooting added patches.

2994.3M16](/packages/vaimo-composer-patches)[lodash-php/lodash-php

A port of Lodash to PHP

527719.0k5](/packages/lodash-php-lodash-php)

PHPackages © 2026

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