Skip to content

Latest commit

 

History

History
31 lines (21 loc) · 787 Bytes

File metadata and controls

31 lines (21 loc) · 787 Bytes

Macros

Macros allow for static class methods to be defined and called at runtime. To begin, add the Titon\Common\Macroable trait to a class.

class Foo {
    use Titon\Common\Macroable;
}
Macros require the __callStatic() magic method to work properly.

Adding A Macro

To add a macro, call the static macro() method with a unique name and a closure to be called.

Foo::macro('lower', ($value) ==> mb_strtolower($value));

Calling A Macro

Once a macro has been added, you can simply call the macro by name as if it was a static method.

Foo::lower('BaR'); // bar

If a macro is called that has not been defined, a Titon\Common\Exception\MissingMacroException is thrown.