PHPackages                             ac/model-traits - 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. [Database &amp; ORM](/categories/database)
4. /
5. ac/model-traits

ActiveLibrary[Database &amp; ORM](/categories/database)

ac/model-traits
===============

Traits to reduce boilerplate when creating models.

0.2.0(12y ago)657623MITPHPPHP &gt;=5.4.0

Since Jan 15Pushed 11y ago5 watchersCompare

[ Source](https://github.com/AmericanCouncils/model-traits)[ Packagist](https://packagist.org/packages/ac/model-traits)[ RSS](/packages/ac-model-traits/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (3)Used By (3)

Model Traits
============

[](#model-traits)

[![Build Status](https://camo.githubusercontent.com/be058dbf00f3f4742664821a730265f5d9f2de7f86eeded99dd5fa2a75ec66f6/68747470733a2f2f7472617669732d63692e6f72672f416d65726963616e436f756e63696c732f6d6f64656c2d7472616974732e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/AmericanCouncils/model-traits)

This is a small convenience library for reusing some common patterns in model classes. Each trait is defined in a section below.

Installing
----------

[](#installing)

Just require `"ac/model-traits": "0.2.0"` in your `composer.json` and run `composer update ac/model-traits`.

AutoGetterSetterTrait
---------------------

[](#autogettersettertrait)

This will use some reflection magic to allow you to use getters/setters for any property, without having to define them yourself. Of course you are still free to explicitly override any getters/setters for properties where you want or need custom logic. Some assumptions are baked into this:

- `public` and `protected` properties are both settable and gettable
- `private` properties are only gettable

Example:

```
use AC\ModelTraits\AutoGetterSetterTrait;

class MyModel
{
    use AutoGetterSetterTrait;

    private $id;
    protected $name;
    protected $age;

    public function __construct($id = null)
    {
        $this->id = $id;
    }
}

$m = new MyModel(5);
$m->setName('John');
$m->setAge(23);

echo $m->getId();       //5
echo $m->getName();     //John
echo $m->getAge();      //23
```

AnnotatedGetterSetterTrait
--------------------------

[](#annotatedgettersettertrait)

If you want to more precisely specify which properties will have getters/setters created, you can use the annotation-based trait:

```
use AC\ModelTraits\AnnotatedGetterSetterTrait;
use AC\ModelTraits\Annotation as MT;

class MyModel
{
    use AnnotatedGetterSetterTrait;

    /**
     * @MT\Getter
     */
    private $id

    /**
     * @MT\GetterAndSetter
     */
    private $name;

    /**
     * @MT\GetterAndSetter
     */
    protected $age;
}
```

Properties can have any combination of access level (public, protected, or private) and accessor methods (getter and/or setter).

ArrayFactoryTrait
-----------------

[](#arrayfactorytrait)

This adds a static factory method to allow creating an instance of a model with a map of property names to values. It does assume that any exhibiting model does not require custom arguments in its constructor. This will allow setting private properties during creation.

Example:

```
use AC\ModelTraits\GetterSetterTrait
use AC\ModelTraits\ArrayFactoryTrait;

class MyModel
{
    use ArrayFactoryTrait, GetterSetterTrait;

    private $id;
    protected $name;
    protected $description;
}

$m = MyModel::createFromArray([
    'id' => 5,
    'name' => 'John',
    'age' => 23
]);

echo $m->getId();       //5
echo $m->getName();     //John
echo $m->getAge();      //23
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 76.5% 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 ~16 days

Total

2

Last Release

4486d ago

### Community

Maintainers

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

---

Top Contributors

[![DavidMikeSimon](https://avatars.githubusercontent.com/u/350396?v=4)](https://github.com/DavidMikeSimon "DavidMikeSimon (13 commits)")[![evillemez](https://avatars.githubusercontent.com/u/318976?v=4)](https://github.com/evillemez "evillemez (4 commits)")

---

Tags

factorymodeltraitgettersetter

### Embed Badge

![Health badge](/badges/ac-model-traits/health.svg)

```
[![Health](https://phpackages.com/badges/ac-model-traits/health.svg)](https://phpackages.com/packages/ac-model-traits)
```

###  Alternatives

[esensi/model

The base model traits of Esensi

20266.5k1](/packages/esensi-model)[greabock/tentacles

Da epic tentacles for Eloquent

3634.4k](/packages/greabock-tentacles)[astrotomic/laravel-cachable-attributes

Allows to cache attribute accessor values in an easy way.

3240.0k](/packages/astrotomic-laravel-cachable-attributes)[reinder83/binary-flags

Useful class for binary operations

1374.5k](/packages/reinder83-binary-flags)

PHPackages © 2026

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