inspira/augmentable
Composer 安装命令:
composer require inspira/augmentable
包简介
Augment PHP class at runtime.
README 文档
README
The Augmentable trait allows for the dynamic addition of methods to a class.
Installation
Install the Augmentable trait using composer:
composer require -s dev inspira/augmentable
Usage
Basic Example
use Inspira\Augmentable\Augmentable; class ExampleClass { use Augmentable; } // Dynamically add a new method named 'customMethod' ExampleClass::augment('customMethod', function () { return 'Custom method implementation'; }); // Use the dynamically added method $instance = new ExampleClass(); $result = $instance->customMethod(); // Outputs: 'Custom method implementation'
Checking for Dynamic Methods
use Inspira\Augmentable\Augmentable; class ExampleClass { use Augmentable; } // Check if a dynamic method named 'customMethod' exists if (ExampleClass::augmented('customMethod')) { $instance = new ExampleClass(); $result = $instance->customMethod(); // Call the method if it exists } else { // Handle the case when the dynamic method does not exist echo 'Dynamic method does not exist.'; }
Listing All Dynamic Methods
use Inspira\Augmentable\Augmentable; class ExampleClass { use Augmentable; } // Get an array of all dynamically added methods $dynamicMethods = ExampleClass::augments(); print_r($dynamicMethods);
Deaugment Added Method
use Inspira\Augmentable\Augmentable; class ExampleClass { use Augmentable; } // Dynamically add a new method named 'customMethod' ExampleClass::augment('customMethod', function () { return 'Custom method implementation'; }); // Remove the 'customMethod' ExampleClass::deaugment('customMethod'); // Remove all methods ExampleClass::deaugment();
统计信息
- 总下载量: 32
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-01-12