PHPackages                             tuupola/witchcraft - 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. tuupola/witchcraft

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

tuupola/witchcraft
==================

Opionated magic methods as traits

1.1.0(9y ago)2315.5k2[1 PRs](https://github.com/tuupola/witchcraft/pulls)1MITPHPPHP &gt;=5.4

Since Oct 17Pushed 8y ago3 watchersCompare

[ Source](https://github.com/tuupola/witchcraft)[ Packagist](https://packagist.org/packages/tuupola/witchcraft)[ Docs](https://github.com/tuupola/witchcraft)[ RSS](/packages/tuupola-witchcraft/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (2)Versions (9)Used By (1)

Witchcraft
==========

[](#witchcraft)

Opionated PHP magic methods as traits.

[![Author](https://camo.githubusercontent.com/b82ff4b35baf0280d673bfc9567e82f8eccf1e4ef1819a0ce4790231cd7d9322/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d40747575706f6c612d626c75652e7376673f7374796c653d666c61742d737175617265)](https://twitter.com/tuupola)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.txt)[![Build Status](https://camo.githubusercontent.com/4854034fab31a6b2660853ca83986e04fc9038352b3fccb0a8278eea3d39982a/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f747575706f6c612f776974636863726166742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/tuupola/witchcraft)[![HHVM Status](https://camo.githubusercontent.com/170317ea9566ec8c13b60e1e58198e4f580788680b76fb626f0332a0d386e6c5/68747470733a2f2f696d672e736869656c64732e696f2f6868766d2f747575706f6c612f776974636863726166742e7376673f7374796c653d666c61742d737175617265)](http://hhvm.h4cc.de/package/tuupola/witchcraft)[![Coverage](https://camo.githubusercontent.com/e12bd542b02747c9ce5a1e8671ac83349eabe1edbbfa20551ff485a063be0a4f/687474703a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f747575706f6c612f776974636863726166742e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/github/tuupola/witchcraft)

Install
-------

[](#install)

You can install latest version using [composer](https://getcomposer.org/).

```
$ composer require tuupola/witchcraft

```

Usage
-----

[](#usage)

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();
$unicorn->setBirthday("1930-24-12")->setColor("rainbow");
print $unicorn->getAge();
```

Magic methods
-------------

[](#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();
$unicorn->birthday("1930-24-12")->color("rainbow");
print $unicorn->age();
```

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

[](#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
---------------

[](#dynamic-methods)

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

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

$unicorn->something("milk");

/* Got milk! */
```

```
$unicorn->something = function ($input) {
    return "No {$input} :(";
};

$unicorn->something("beer");

/* No beer :() */
```

Why?
====

[](#why)

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

Testing
-------

[](#testing)

You can run tests either manually...

```
$ composer test
```

... or automatically on every code change. You will need [entr](http://entrproject.org/) for this to work.

```
$ brew install entr
$ composer watch
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

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

License
-------

[](#license)

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

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity63

Established project with proven stability

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

Recently: every ~139 days

Total

7

Last Release

3508d ago

Major Versions

0.3.0 → 1.0.02016-02-29

PHP version history (2 changes)0.1.0PHP ~5.4

0.2.1PHP &gt;=5.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/3325405a7d8a43bc40dd0e760a4b7f268fba32a7150cf0327f64f13d1661df0b?d=identicon)[tuupola](/maintainers/tuupola)

---

Top Contributors

[![tuupola](https://avatars.githubusercontent.com/u/21913?v=4)](https://github.com/tuupola "tuupola (46 commits)")

---

Tags

magicphptraitmethodsmagicwitchcraft

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/tuupola-witchcraft/health.svg)

```
[![Health](https://phpackages.com/badges/tuupola-witchcraft/health.svg)](https://phpackages.com/packages/tuupola-witchcraft)
```

###  Alternatives

[danielstjules/stringy

A string manipulation library with multibyte support

2.4k26.0M191](/packages/danielstjules-stringy)[voku/arrayy

Array manipulation library for PHP, called Arrayy!

4875.5M16](/packages/voku-arrayy)[voku/stringy

A string manipulation library with multibyte support

1783.8M19](/packages/voku-stringy)[statamic/stringy

A string manipulation library with multibyte support, forked from @statamic

234.5M14](/packages/statamic-stringy)[lochmueller/autoloader

Automatic components loading of ExtBase extensions to get more time for coffee in the company ;) This ext is not a PHP SPL autoloader or class loader - it is better! Loads CommandController, Xclass, Hooks, FlexForms, Slots, TypoScript, TypeConverter, BackendLayouts and take care of createing needed templates, TCA configuration or translations at the right location.

19364.9k5](/packages/lochmueller-autoloader)[tcb13/substringy

A sub string manipulation library with multibyte support that extends Stringy

1760.6k1](/packages/tcb13-substringy)

PHPackages © 2026

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