coco-project / macroable
Provide a trait that dynamically injects a specified method or closure into a specified object during runtime execution.
v1.0.4
2024-01-06 07:19 UTC
Requires
- php: >=8.0
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^3.8
README
Provide a trait that dynamically injects a specified method or closure into a specified object during runtime execution.
Here's a quick example:
<?php class A { private int $val = 1; use \Coco\macroable\Macroable; } $obj = new A(); $obj::injectMethod('add', function($num) { return $this->val + $num; }); echo $obj->add(10); //11
Installation
You can install the package via composer:
composer require coco-project/macroable
Usage
For more examples, please refer to the "examples" folder.
You can add a new method to a class using injectMethod
:
class A { private int $val = 1; private static int $val2 = 2; use \Coco\macroable\Macroable; } $obj = new A(); $obj::injectMethod('add', function() { return static::$val2 + 10; }); echo $obj::add();
Testing
composer test
License
MIT