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

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

clouding/has-attributes
=======================

A trait to give class attributes

v0.1.1(7y ago)629MITPHPPHP &gt;=7.2

Since Mar 23Pushed 7y ago1 watchersCompare

[ Source](https://github.com/cloudingcity/has-attributes)[ Packagist](https://packagist.org/packages/clouding/has-attributes)[ RSS](/packages/clouding-has-attributes/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (3)Dependencies (3)Versions (4)Used By (0)

Has Attributes Trait
====================

[](#has-attributes-trait)

[![](https://camo.githubusercontent.com/edbf26e39c15318cd1fdd376545759e7c294637e1392b26d3023a163130b4e64/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f636c6f7564696e672f6861732d617474726962757465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/clouding/has-attributes)[![](https://camo.githubusercontent.com/89fa4fc735ae39aa471a00cc5993d697f13a3f19dc37c8b6a03032c8ad26a3b2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f636c6f7564696e67636974792f6861732d617474726962757465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/clouding/has-attributes)[![](https://camo.githubusercontent.com/45b3d543d2fdd8bbdb332553e9ee72615fd1841b167a7ab283937d58c961ae9a/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6d2f636c6f7564696e67636974792f6861732d617474726962757465732e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.com/cloudingcity/has-attributes)[![](https://camo.githubusercontent.com/9a9e597897ee6dd13b6d22a10a38bbb0b038491f8f3da15a115a1a8cda5c1abf/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f636c6f7564696e67636974792f6861732d617474726962757465732e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/gh/cloudingcity/has-attributes)

A trait to give class attributes

Features
--------

[](#features)

- Give class attributes with array.
- Define attributes strict type.

Quick Example
-------------

[](#quick-example)

Just use `HasAttributes` trait and pass key value array to constructor

```
class Post
{
    use \Clouding\HasAttributes\HasAttributes;
}

$post = new Post([
    'title' => 'Hello',
    'body' => 'World',
]);

echo $post->title; // Hello
echo $post->body;  // World
```

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

[](#installation)

```
composer require clouding/has-attributes

```

Usage
-----

[](#usage)

### Define type

[](#define-type)

Declare `$define` to define type

```
require 'vendor/autoload.php';

interface Eatable {}

class Pig implements Eatable { }

class Zoo
{
    use \Clouding\HasAttributes\HasAttributes;

    protected $define = [
        'name' => 'string',
        'number' => 'int',
        'animal' => Eatable::class,
    ];
}

$zoo = new Zoo([
    'name' => 'Mike',
    'number' => 100,
    'animal' => new Pig(),
]);

echo $zoo->name;              // Mike
echo $zoo->number;            // 100
echo get_class($zoo->animal); // Pig
```

If you declare `$define` property, it will check type strictly

```
new Zoo(['name' => 999]);

// InvalidArgumentException: [name => 999] value is not equal to define type [string]
```

And can't set key that is not defined

```
new Zoo(['foo' => 'bar']);

// InvalidArgumentException: Key [foo] is not defined
```

Supported `$define` type:

- `string`
- `int`, `integer`
- `bool`, `boolean`
- `object`
- `array`
- `real`, `float`, `double`
- Class, Interface

### Set attributes

[](#set-attributes)

There have many way to set attributes

```
class Person
{
    use \Clouding\HasAttributes\HasAttributes;
}

$person = new Person([
    'id' => 100,
]);

$person->setAttributes([
    'name' => 'Marry',
    'phone' => '0912345678'
]);

$person->setAttribute('sex', 'female');

$person->age = 18;

echo $person->id;    // 100
echo $person->name;  // Marry
echo $person->phone; // 0912345678
echo $person->sex;   // female
echo $person->age;   // 18
```

### Get attributes

[](#get-attributes)

```
class Person
{
    use \Clouding\HasAttributes\HasAttributes;
}

$person = new Person([
    'id' => 100,
    'name' => 'Jack',
]);

echo $person->getAttribute('id');                 // 100

echo $person->getAttribute('salary', 0);          // 0 (if key not exists return default value)

var_dump($person->getAttributes('id', 'name'));   // ['id' => 100, 'name' => 'Jack']

var_dump($person->getAttributes(['id', 'name'])); // ['id' => 100, 'name' => 'Jack']
```

###  Health Score

24

—

LowBetter than 31% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

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

Total

3

Last Release

2654d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/11569651?v=4)[Clouding](/maintainers/cloudingcity)[@cloudingcity](https://github.com/cloudingcity)

---

Top Contributors

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

---

Tags

attributephptraittraitattribute

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[nette/robot-loader

🍀 Nette RobotLoader: high performance and comfortable autoloader that will search and autoload classes within your application.

89454.3M347](/packages/nette-robot-loader)[kkszymanowski/traitor

Add a trait use statement to existing PHP class

1305.5M16](/packages/kkszymanowski-traitor)[koriym/attributes

An annotation/attribute reader

433.6M14](/packages/koriym-attributes)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2518.6k](/packages/iteks-laravel-enum)[gomachan46/state-machine

simple state machine with annotations for PHP, inspired by AASM known as a Ruby state machine.

1896.3k](/packages/gomachan46-state-machine)[ebidtech/collection

A set of interfaces and traits to speed up the creation of collections

13122.3k6](/packages/ebidtech-collection)

PHPackages © 2026

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