Simulation of the Python decorators in PHP
Install with composer: composer require zhikiri/pdecorate
Creation of the new decorator, second parameter must be callable.
Allow to use class methods, function names and Closure class instances (anonymous function)
Pdecorate::add('italic', function ($content) {
return "<i>{$content}</i>";
});Get instance of the decoration
First of all pass the decorators and the last parameter must be the callable function.
$decoration = new Decorator(
'italic',
function () {
return 'decoration text';
}
);Decoration execution:
- cast to string
(string)$decoration - execute the Decorator instance
$decoration() - run the Decorator call method
$decoration->call()
Result of the current decoration will be: <i>decoration text</i>