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

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

thinktomorrow/magic-attributes
==============================

Magic assistance for retrieving nested properties

0.2.0(7y ago)11.4k↓80%MITPHPPHP ^7.1

Since Sep 11Pushed 5y ago2 watchersCompare

[ Source](https://github.com/thinktomorrow/magic-attributes)[ Packagist](https://packagist.org/packages/thinktomorrow/magic-attributes)[ Docs](https://github.com/thinktomorrow/magic-attributes)[ RSS](/packages/thinktomorrow-magic-attributes/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (4)Versions (3)Used By (0)

**This repository is archived in favor of our new dynamic-attributes repository which provides the same behaviour. Please consider using this package instead since magic attributes will no longer be maintained.**

Magic attributes
================

[](#magic-attributes)

Retrieve nested property values via dot syntax.

[![Latest Version on Packagist](https://camo.githubusercontent.com/9ed41340e0799351813e1932db7be51320291cb3ab74a0efcb632fa1c34b799b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7468696e6b746f6d6f72726f772f6d616769632d617474726962757465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/thinktomorrow/magic-attributes)[![Build Status](https://camo.githubusercontent.com/a807a34e8c503abd1b66ddfbd82d589c94b8dac30e448a4116cdfe6fc3c83b0f/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7468696e6b746f6d6f72726f772f6d616769632d617474726962757465732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/thinktomorrow/magic-attributes)[![StyleCI](https://camo.githubusercontent.com/f8220ed6fd2b99d48058f0b714156591caedf85be2ce7eb0e12f3d816fbebc8c/68747470733a2f2f7374796c6563692e696f2f7265706f732f3134343832323231302f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/144822210)[![Quality Score](https://camo.githubusercontent.com/beb3854bbe2ad2176e06f50bb711ebc28f5c83e2e43dc763b1a26d86cf89eb7c/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7468696e6b746f6d6f72726f772f6d616769632d617474726962757465732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/thinktomorrow/magic-attributes)[![Total Downloads](https://camo.githubusercontent.com/23b4bbf11a798189f4eae2474b4ea2ee5ab5d153ceb97c4a3ee02fb3700403fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7468696e6b746f6d6f72726f772f6d616769632d617474726962757465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/thinktomorrow/magic-attributes)

Working with multi-level arrays or objects, it can sometimes prove to be an annoyance to get just that deeper lying property value. You need to manually go through each level. This package provides an easy api for fetching these nested values. Instead of doing something like this:

```
if(!isset($class->foo)) return null;
if(!isset($class->foo->bar)) return null;

return $class->foo->bar;
```

With this package you could do it like this:

```
return $class->attr('foo.bar');
```

Trait
-----

[](#trait)

In order to add the functionality, you need to add a trait to your class. Here's an example:

```
use \Thinktomorrow\MagicAttributes\HasMagicAttributes;

class Customer{
    use HasMagicAttributes;
}
```

Magic properties
----------------

[](#magic-properties)

If you'd like to fetch your values as if they are top level properties of your class, you could setup a `__get` and `__isset` method in order to accomplish this. It can look something like this:

```
class Customer{

    /* allows for $customer->addressStreet instead of $customer->attr('address.street') */
    public function __get($key)
    {
        return $this->magicAttribute($key);
    }

    /* with __isset you allow to check if the property exists on this class, e.g. isset($customer->addressStreet) */
    public function __isset($key)
    {
        return false !== $this->magicAttribute($key, false);
    }

}
```

Strict retrieval
----------------

[](#strict-retrieval)

The package provides a single point of entry so by default you retrieve values via calling the `attr` method, e.g. `$class->attr('foo.bar')`. The benefit here is that this method is highly recognizable in the public api usage. The downside is that it does not strictly protects your class api and properties. One way of dealing with this is restricting the public usage of the `attr` method and providing your own public api.

Within the MagicAttributes trait, you will find a `disallow_magic_api` property which defaults to false. This should be set to true in order to prevent public usage of the `attr` method. Any attempt in using this method, will now throw an `DisallowedMagicAttributeUsage` exception.

In your class you can make use of the `magicAttribute` method which has the same signature as the `attr` method but which is a protected method and can only be used by the internal api.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Ben Cavens](https://github.com/bencavens)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity48

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

Total

2

Last Release

2646d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/497668?v=4)[Ben Cavens](/maintainers/BenCavens)[@BenCavens](https://github.com/BenCavens)

---

Top Contributors

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

---

Tags

think tomorrowmagic-attributes

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/thinktomorrow-magic-attributes/health.svg)

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

###  Alternatives

[paragonie/stern

Stern lets you built type-safe PHP projects, even if your project's users aren't writing type-safe code

201.5k1](/packages/paragonie-stern)

PHPackages © 2026

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