jhonymiler/di
Composer 安装命令:
composer require jhonymiler/di
包简介
Dependency Injection Container
README 文档
README
Container Usage Documentation
Container Usage Documentation
The Container class is a basic dependency injection container that allows you to manage dependencies and resolve them automatically within your application. It provides a convenient way to handle object instantiation and dependency injection, making your code more modular and maintainable.
Getting Started
To begin using the Container class, you first need to include the relevant PHP files and create an instance of the container:
use Tests\Core\Container; require 'vendor/autoload.php'; $container = Container::getInstance();
Binding Classes to the Container
You can bind classes or closures to the container using the bind method. When you request an instance from the container, it will automatically resolve the bound dependencies for you.
// Binding a customized MysqlDb instance to the container $container->bind(MysqlDb::class, function () { // Custom configuration for MysqlDb, if needed $db = new Db(/*...*/); return new MysqlDb($db); });
Resolving Dependencies
When you call the get method with a class name, the container will resolve and return an instance of that class, along with its dependencies.
// Retrieving an instance of MysqlDb from the container $mysqlDb = $container->get(MysqlDb::class); // Retrieving an instance of User from the container $user = $container->get(User::class);
Dependency Injection
The container can automatically inject dependencies into closures when using the call method. This allows you to resolve dependencies for a specific function or method.
// Using the call method to automatically resolve dependencies for a closure $container->call(function (Logger $logger, EntityManager $em) use ($user) { // Here, the logUserData method of the User class will be called with resolved dependencies $user->logUserData($logger); $data = $em->getData(); var_dump($data); });
Error Handling
If a class is not found in the container, or if there are unresolved dependencies, the Container class will throw an Exception. It is important to handle these exceptions properly in your application to ensure smooth operation.
try { $invalidInstance = $container->get(NonExistentClass::class); } catch (Exception $e) { // Handle the exception here }
Conclusion
The Container class simplifies the management of dependencies in your PHP application. By binding classes and closures, you can easily resolve instances with their dependencies, making your code more organized and easier to maintain.
jhonymiler/di 适用场景与选型建议
jhonymiler/di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 07 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jhonymiler/di 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jhonymiler/di 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-23