Dynamic Method Trait

trait DynamicMethodTrait


Adds ability to add methods into objects dynamically. That’s like a “trait” feature of a PHP, but implemented in run-time:

$object->addMethod('test', function ($o, $args) {
    echo 'hello, ' . $args[0];

Global Methods

If object has application scope AppScopeTrait and the application implements HookTrait then executing $object->test() will also look for globally-registered method inside the application:

$object->getApp()->addGlobalMethod('test', function ($app, $o, $args) {
    echo 'hello, ' . $args[0];


Of course calling test() on the other object afterwards will trigger same global method.

If you attempt to register same method multiple times you will receive an exception.

Dynamic Method Arguments

When calling dynamic method first argument which is passed to the method will be object itself. Dynamic method will also receive all arguments which are given when you call this dynamic method:

$m->addMethod('sum', function ($m, $a, $b) {
    return $a + $b;
echo $m->sum(3, 5); // 8




DynamicMethodTrait::tryCall($method, $arguments)

Tries to call dynamic method, but doesn’t throw exception if it is not possible.

DynamicMethodTrait::addMethod($name, $closure)

Add new method for this object. See examples above.


Returns true if object has specified method (either native or dynamic). Returns true also if specified methods is defined globally.


Remove dynamically registered method.

DynamicMethodTrait::addGlobalMethod($name, $closure)

Registers a globally-recognized method for all objects.


Return true if such global method exists.