, (*1)
A trait to dynamically add methods to a class
, (*2)
This package provides a trait that, when applied to a class, makes it possible to add methods to that class at runtime., (*3)
Here's a quick example:, (*4)
$myClass = new class() {
use Spatie\Macroable\Macroable;
};
$myClass::macro('concatenate', function(... $strings) {
return implode('-', $strings);
});
$myClass->concatenate('one', 'two', 'three'); // returns 'one-two-three'
The idea of a macroable trait and the implementation is taken from the macroable
trait of the Laravel framework., (*5)
Support us
, (*6)
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products., (*7)
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall., (*8)
Installation
You can install the package via composer:, (*9)
composer require spatie/macroable
Usage
You can add a new method to a class using macro
:, (*10)
$macroableClass = new class() {
use Spatie\Macroable\Macroable;
};
$macroableClass::macro('concatenate', function(... $strings) {
return implode('-', $strings);
});
$macroableClass->concatenate('one', 'two', 'three'); // returns 'one-two-three'
Callables passed to the macro
function will be bound to the class
, (*11)
$macroableClass = new class() {
protected $name = 'myName';
use Spatie\Macroable\Macroable;
};
$macroableClass::macro('getName', function() {
return $this->name;
};
$macroableClass->getName(); // returns 'myName'
You can also add multiple methods in one go by using a mixin class. A mixin class contains methods that return callables. Each method from the mixin will be registered on the macroable class., (*12)
$mixin = new class() {
public function mixinMethod()
{
return function() {
return 'mixinMethod';
};
}
public function anotherMixinMethod()
{
return function() {
return 'anotherMixinMethod';
};
}
};
$macroableClass->mixin($mixin);
$macroableClass->mixinMethod() // returns 'mixinMethod';
$macroableClass->anotherMixinMethod() // returns 'anotherMixinMethod';
Changelog
Please see CHANGELOG for more information on what has changed recently., (*13)
Testing
bash
composer test
, (*14)
Contributing
Please see CONTRIBUTING for details., (*15)
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker., (*16)
Postcardware
You're free to use this package (it's MIT-licensed), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using., (*17)
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium., (*18)
We publish all received postcards on our company website., (*19)
Credits
Idea and code is taken from the macroable
trait of the Laravel framework., (*20)
License
The MIT License (MIT). Please see License File for more information., (*21)