drewlabs/overloadable
Composer 安装命令:
composer require drewlabs/overloadable
包简介
Provides function overloading to PHP language. It's an experimental feature as it may not be optimized for all use-case
README 文档
README
PHP Library that provides Object Oriented method overloading to PHP class definition. It uses the power of PHP reflexion API to find and call overloaded methods.
Note The implementation is experimental as it might not be fully optimize for every use case, and may cause performance heck to your application when multiple overload method is provided.
Installation
The recommended way to include the package into your project is by using PHP composer package manager.
In a composer.json at your project or library root folder add the following:
{
// ...
"require": {
"drewlabs/overloadable": "^0.1.0"
}
// ...
}
Usage
- Inline method overloading:
// ... use Drewlabs\Overloadable\Overloadable; class TestClass { use Overloadable; public function log(...$args) { return $this->overload($args, [ static function (ConsoleLogger $logger) { return $logger->log(); }, static function (FileLogger $logger, ?string $prefix = null) { return $logger->log($prefix ?? 'ERROR024'); }, ]); } }
- Class method overload
// ... use Drewlabs\Overloadable\Overloadable; class TestClass { use Overloadable; public function log(...$args) { return $this->overload($args, [ 'log1', 'log2' ]); } private function log1(ConsoleLogger $logger) { return $logger->log(); } private function log2(FileLogger $logger, ?string $prefix = null) { return $logger->log($prefix ?? 'ERROR024'); } }
After library installation is completed, in order to use the overloadable implementations, include the composer autoload file in your project entry script. The example below assume your project entry script is index.php :
// index.php // Load composer autoloaded php scripts using PSR4 implementation require_once __DIR__ 'vendor/autoload.php'; // Calling the overloaded methods $object = new TestClass(); $object->log(new FileLogger); // Call the overload that accept an instance of FileLogger as 1st parameter $object->log(new ConsoleLogger); // Call the overload that accept an instance of ConsoleLogger as 1st parameter $object->log(new ConsoleLogger, 'ERROR CODE 2308'); // Throws execption as there is no overloaded function that matches
drewlabs/overloadable 适用场景与选型建议
drewlabs/overloadable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 611 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 drewlabs/overloadable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 drewlabs/overloadable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 611
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 5
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-04