Sorry, we don't support your browser.  Install a modern browser

Extend the plugin system to allow all callables, not just anonymous functions

Multiple parts of the system allow you to provide callbacks to extend core components, like hooks and collection methods. Kirby uses instance binding to bind objects (like the $kirby object or the collection for collection methods) so those can be accessed from the callback using $this. Because of this, you can only pass anonymous functions, because other closures can’t be bound to a new context. This makes it impossible to structure your code differently, for example by putting all your collection methods and hooks into separate classes and using static methods:

use Kirby\Cms\App;

App::plugin('site/collection-methods', [
    'collectionMethods' => [
        'myCustomMethod' => CollectionMethods::myCustomMethod(...),
    ],
]);

This throws an error because of the late static binding. The same applies to instance methods and named functions.

Introducing a separate syntax to be able to specify arbitrary callables for those plugins would allow more freedom for site and plugin authors to structure their code.

Any solution to this should work uniformly across all of Kirby’s components. See this issue for the related challenges and proposed solutions.

3 years ago
2