PHPackages                             aesonus/php-magic - 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. aesonus/php-magic

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

aesonus/php-magic
=================

Reads the class docblock to determine magic method behavior

0.4.3(5y ago)0314MITPHPPHP ^7.2

Since Feb 24Pushed 5y agoCompare

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

READMEChangelog (10)Dependencies (3)Versions (11)Used By (0)

PHP Magic
=========

[](#php-magic)

---

[![Build Status](https://camo.githubusercontent.com/aee94a83255318a660173359b6a0ef14cd9b00bed5b546b06610b70363072b02/68747470733a2f2f7472617669732d63692e6f72672f4165736f6e75732f7068702d6d616769632e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Aesonus/php-magic)

Reads the class docblock to validate and get, set, isset, or unset defined inaccessible object properties using \_\_get, \_\_set, \_\_isset, and \_\_unset.

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

[](#installation)

---

Use composer to install:

```
composer require aesonus/php-magic
```

Usage
-----

[](#usage)

---

Basic Use Case:

1. Import the trait into your class

```
class MyClass {
    use HasMagicProperties;
    // or
    use HasInheritedMagicProperties;
}
```

2. Add properties to docblock using phpdoc formatted docblocks and define object properties of the same name

```
use \stdClass;

/**
 * Yup, the question mark can be used to indicate null
 * @property ?string $testStringOrNullProperty Optional description
 * The | can validate using 'or'
 * @property float|string   $testFloatOrStringProperty
 * @property-read int $testIntReadProperty
 * @property-read callable|object $testCallableOrObjectReadProperty
 * @property-write stdClass|null $testStdClassOrNullWriteProperty
 * @property-write mixed $testMixedWriteProperty
 */
class MyClass {
    use HasMagicProperties;

    protected $testStringOrNullProperty;
    protected $testFloatOrStringProperty;
    protected $testIntReadProperty;
    protected $testCallableOrObjectReadProperty;
    protected $testStdClassOrNullWriteProperty;
    protected $testMixedWriteProperty;
    ...
}
```

3. Create magic methods and make them call the corresponding magic\[Get|Isset|Set|Unset\] methods (TIP: There is no need to implement all the magic methods if you don't need to)

In this example we set up the magic property methods ourselves

```
/**
* ...
*/
class MyClass {
    use HasMagicProperties;

    /**
    * Only readable properties will be read (@property or @property-read)
    */
    public function __get($name)
    {
        return $this->magicGet($name);
    }

    /**
    * Only readable properties will be read (@property or @property-read)
    */
    public function __isset($name)
    {
        return $this->magicIsset($name);
    }

    /**
    * Only writable properties will be set (@property or @property-write)
    */
    public function __set($name, $value)
    {
        $this->magicSet($name, $value);
    }

    /**
    * Only writeable properties will be set (@property or @property-write)
    */
    public function __unset($name)
    {
        $this->magicUnset($name);
    }

    ...
}
```

You may also use the ImplementsMagicMethods trait to just implement all of the magic functions automatically

```
/**
* ...
*/
class MyClass {
    use HasMagicProperties;
    use ImplementsMagicMethods;
    ...
}
```

4. You can also define custom behavior in your magic methods if you so choose

```
/**
 * ...
 */
class MyClass {

    public function __get($name)
    {
        // Do something
        return $this->magicGet($name);
    }

    public function __set($name, $value)
    {
        $this->magicSet($name, $value);
        //Manipulate the set value after the validation, if wanted
    }

    ...
}
```

Type Validation
---------------

[](#type-validation)

---

The trait will use the type(s) defined in the doc block to validate the input. It currently supports any type that can be called by is\_int, is\_bool, etc; and any class that you may desire.

The following example validates the parameter as a class My\\Namespace\\Foo or My\\Other\\Namespace\\Bar

```
use My\Namespace\Foo;
use My\Other\Namspace\Bar;

/**
 *
 * @property $myClass Foo|Bar
 */
class MyClass {

}
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity51

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

Recently: every ~119 days

Total

9

Last Release

2116d ago

### Community

Maintainers

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

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/aesonus-php-magic/health.svg)

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

###  Alternatives

[dsposito/argo

A shipping utility.

23177.0k](/packages/dsposito-argo)

PHPackages © 2026

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