crowdstar/reflection
Composer 安装命令:
composer require crowdstar/reflection
包简介
Allow to directly access protected/private properties and call protected/private methods.
README 文档
README
A PHP reflection library to directly access protected/private properties and call protected/private methods. Static properties and methods can also be accessed/invoked from a class directly.
This library works with major versions of PHP from 5.3 to 8.5.
Installation
For PHP 8.0+, please use version 2.0:
composer require crowdstar/reflection:~2.0.0
For old versions of PHP (PHP 5.3 to PHP 7.4), please use version 1.0 instead:
composer require crowdstar/reflection:~1.0.0
How To Use It
Three static methods are provided to access protected/private properties and call protected/private methods of an object/class:
CrowdStar\Reflection\Reflection::getProperty(): Get current value of a property.CrowdStar\Reflection\Reflection::setProperty(): Set a new value to a property.CrowdStar\Reflection\Reflection::callMethod(): Call a method of a class/object.
Here is a sample code showing how to use them:
<?php declare(strict_types=1); require __DIR__ . '/vendor/autoload.php'; use CrowdStar\Reflection\Reflection; class Helper { private string $key; private static string $keyStatic; private function get(): string { return 'Private method invoked.'; } private static function getStatic(int $i, int $j): string { return "Private static method invoked with parameter {$i} and {$j}."; } } $helper = new Helper(); // Access properties and invoke methods from objects. Reflection::setProperty($helper, 'key', 'Set value to a private property.'); echo "Output 1: ", Reflection::getProperty($helper, 'key'), PHP_EOL; echo "Output 2: ", Reflection::callMethod($helper, 'get'), PHP_EOL, PHP_EOL; // Access static properties and invoke static methods from objects. Reflection::setProperty($helper, 'keyStatic', 'Set value to a private static property.'); echo "Output 3: ", Reflection::getProperty($helper, 'keyStatic'), PHP_EOL; echo "Output 4: ", Reflection::callMethod($helper, 'getStatic', [1, 2]), PHP_EOL, PHP_EOL; // Static properties and methods can also be accessed/invoked from a class directly. Reflection::setProperty(Helper::class, 'keyStatic', 'Set another value to a private static property.'); echo "Output 5: ", Reflection::getProperty(Helper::class, 'keyStatic'), PHP_EOL; echo "Output 6: ", Reflection::callMethod(Helper::class, 'getStatic', [3, 4]), PHP_EOL; ?>
crowdstar/reflection 适用场景与选型建议
crowdstar/reflection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.92k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 06 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 crowdstar/reflection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 crowdstar/reflection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 20.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 22
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2018-06-25