承接 jhonymiler/di 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 jhonymiler/di 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-23