lithemod / orbis
PHP中轻松处理实例的强大类管理器!
v1.0.0
2024-10-02 16:54 UTC
Requires (Dev)
- phpunit/phpunit: ^11.3
This package is auto-updated.
Last update: 2024-10-02 16:55:54 UTC
README
✨ Lithe Orbis 是一个在PHP中轻松且高效管理类实例的强大类!
安装
您可以使用Composer安装 Lithe Orbis。在您的终端中运行以下命令
composer require lithemod/orbis
使用方法
注册实例
要注册类的实例,您可以使用 register
方法。以下是一个示例
use Lithe\Orbis\Orbis; class MyClass { public function sayHello() { return "Hello, World!"; } } // Register a new instance without a key Orbis::register(MyClass::class); // Get the registered instance $instance = Orbis::instance(MyClass::class); echo $instance->sayHello(); // Output: Hello, World!
使用密钥注册
您还可以使用自定义密钥注册实例
$myObject = new MyClass(); Orbis::register($myObject, 'myCustomKey'); // Get the instance using the custom key $instance = Orbis::instance('myCustomKey'); echo $instance->sayHello(); // Output: Hello, World!
注销实例
如果您需要删除已注册的实例,可以使用 unregister
方法
Orbis::unregister(MyClass::class);
使用注销获取实例
instance
方法有一个可选的第二个参数,当设置为 true
时,在返回实例时会注销该实例。以下是它的用法
// Register the instance Orbis::register(MyClass::class); // Get and unregister the instance $instance = Orbis::instance(MyClass::class, true); echo $instance->sayHello(); // Output: Hello, World! // The instance is now unregistered
常见错误
- 未注册的类:如果您尝试获取尚未注册的实例,将会抛出错误。
- 实例已注册:尝试使用现有密钥注册实例将抛出错误。
贡献
欢迎贡献!请随意提交拉取请求或打开问题。
许可证
本项目采用MIT许可证。有关详细信息,请参阅LICENSE 文件。