This package is abandoned and no longer maintained. No replacement package was suggested.

Opionated magic methods as traits

Fund package maintenance!

1.1.0 2016-10-08 19:27 UTC

This package is auto-updated.

Last update: 2022-04-12 06:48:36 UTC


Opionated PHP magic methods as traits.

Author Software License Build Status HHVM Status Coverage


You can install latest version using composer.

$ composer require tuupola/witchcraft


You have your usual class with boilerplate accessors and mutators.

class Unicorn
    private $color;
    private $birthday;

    public function __construct($color = "white", $birthday = null)
        $this->color = $color;
        $this->birthday = $birthday;

    public function getColor()
        return $this->color;

    public function setColor($color)
        $this->color = $color;
        return $this;

    public function getBirthday()
        return $this->birthday;

    public function setBirthday($birthday)
        $this->birthday = DateTime::createFromFormat("Y-m-d", $birthday);
        return $this;

    public function getAge()
        $now = new DateTime();
        return $this->birthday->diff($now)->format("%y years");

It all works really nice with ide autocompletes and everything. Problem is your code looks quite ugly.

$unicorn = new Unicorn();
print $unicorn->getAge();

Magic methods

Witchcraft to the resque. If you add Witchcraft\MagicMethods trait you can use pretty methods.

class Unicorn
    use \Witchcraft\MagicMethods;

    /* Rest of the code stays exactly the same. */
$unicorn = new Unicorn();
print $unicorn->age();

Magic properties

If you add Witchcraft\MagicProperties trait you can use pretty properties.

class Unicorn
    use \Witchcraft\MagicProperties;

    /* Rest of the code stays exactly the same. */
$unicorn = new Unicorn();
$unicorn->birthday = "1930-24-12";
$unicorn->color = "rainbow";
print $unicorn->age;

Dynamic methods

As a bonus you can dynamically assing methods to the object.

$unicorn->something(function ($input) {
    return "Got {$input}!";


/* Got milk! */
$unicorn->something = function ($input) {
    return "No {$input} :(";


/* No beer :() */


Because I think getFoo() and setFoo("bar") are ugly.


You can run tests either manually...

$ composer test

... or automatically on every code change. You will need entr for this to work.

$ brew install entr
$ composer watch


Please see CONTRIBUTING for details.


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


The MIT License (MIT). Please see License File for more information.