christophrumpel/method-overrider
Composer 安装命令:
composer require christophrumpel/method-overrider
包简介
A PHP package to override methods.
关键字:
README 文档
README
PHP Method Overrider
⚠️ Experimental Package - This is a work in progress and not intended for production use.
Dynamically override specific methods of any PHP class at runtime while preserving method signatures and providing access to the original implementation.
Installation
composer require christophrumpel/method-overrider
Usage
use ChristophRumpel\MethodOverrider\MethodOverrider; class MathService { public function add(int $a, int $b): int { return $a + $b; } } $overrider = new MethodOverrider(); // Override the add method to multiply instead $instance = $overrider->override( class: MathService::class, methodNames: 'add', implementations: function (callable $original, int $a, int $b): int { // You can still call the original method $originalResult = $original(); // Or implement completely new logic return $a * $b; } ); echo $instance->add(2, 3); // Output: 6 (multiplied instead of added)
How it works
The package uses reflection to analyze method signatures and dynamically generates new classes that extend your original class. The overridden methods wrap your custom implementations while providing access to the original method via a closure.
Note: The override() method uses eval() to dynamically create and instantiate the overridden class. If you prefer to avoid eval(), see the alternative method below.
Alternative: Generate Class Without eval()
If you want to avoid eval(), you can use the generateOverriddenClass() method which returns the generated class code as a string along with metadata:
$result = $overrider->generateOverriddenClass( class: MathService::class, methodNames: 'add', implementations: function (callable $original, int $a, int $b): int { return $a * $b; } ); // Returns an array with: // - 'content': The generated PHP class code as a string // - 'implementations': The provided implementations array // - 'className': The generated class name // You can then save this to a file or handle it however you prefer file_put_contents('path/to/MathServiceCacheProxy.php', $result['content']);
Requirements
- PHP 8.3+
christophrumpel/method-overrider 适用场景与选型建议
christophrumpel/method-overrider 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 10 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「methods」 「overriding」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 christophrumpel/method-overrider 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 christophrumpel/method-overrider 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 christophrumpel/method-overrider 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Collection of methods that I or you usually use.
DEPRECATED - Provides means to dynamically deal with inaccessible properties, by implementing some of PHP's magic methods
Laravel Utilities
Alfabank REST API integration
Helpful library for operating with URI, Arrays, Dates, Links and Text.
A fork of danielstjules/Stringy fixing php 7.3 compatibility and greek letter Θ being slugified to O instead of ΤΗ. A string manipulation library with multibyte support
统计信息
- 总下载量: 47
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-29