zhikiri / pdecorate
Python like function decoration in PHP
Installs: 9
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/zhikiri/pdecorate
Requires (Dev)
- phpunit/phpunit: ^6.5
This package is not auto-updated.
Last update: 2022-12-10 20:32:48 UTC
README
Simulation of the Python decorators in PHP
Install with composer: composer require zhikiri/pdecorate
Description
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>