PHP-DI

人类的依赖注入容器。(The dependency injection container for humans.)

Github星跟踪图

PHP-DI是一个依赖注入容器,意味着实用、强大和框架无关。

PHP-DI使用PHP的Reflection类,这是非常标准的,Laravel、Zend Framework和许多其他容器也是同样做的。 性能方面,这样的信息被读取一次然后缓存,它没有影响。

PHP-DI有3种方法来定义在类中注入什么:

  • 使用自动装配(autowiring)
  • 使用注释(annotations)
  • 使用PHP定义(PHP definitions)

每一个都是不同的和可选的。 以下是一个文件中的PHP定义的例子:

return [    'api.url'    => 'http://api.example.com',    'Webservice' => function (Container $c) {        return new Webservice($c->get('api.url'));    },    'Controller' => DI\object()        ->constructor(DI\get('Webservice')),];

但是我们不想在我们的应用程序中调用容器:它将我们的代码耦合到容器。 这被称为服务定位器反模式或依赖性提取而不是注入。

引用Symfony文档中所述:

You will need to get [an object] from the container at some point but this should be as few times as possible at the entry point to your application.
(你需要在某个点从容器中获取(get)一个对象,但在您应用程序的入口点次数应尽可能少。)

因此,PHP-DI与一些框架集成,这样您不必调用容器(依赖注入到控制器中):

  • Symfony
  • Silex
  • Zend Framework 1
  • Zend Framework 2
  • Silly

概览

名称与所有者PHP-DI/PHP-DI
主编程语言PHP
编程语言PHP (语言数: 6)
平台
许可证MIT License
发布数111
最新版本名称7.0.6 (发布于 )
第一版名称0.9.0 (发布于 )
创建于2012-03-17 23:59:36
推送于2024-04-24 17:58:01
最后一次提交2024-04-18 13:29:02
星数2.6k
关注者数82
派生数312
提交数1.9k
已启用问题?
问题数516
打开的问题数47
拉请求数284
打开的拉请求数10
关闭的拉请求数73
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?

layout: home

Build Status
Coverage Status
Downloads per months
Total downloads

PrettyCI Status

Average time to resolve an issue
Percentage of issues still open

PHP-DI is a dependency injection container meant to be practical, powerful, and framework-agnostic.

Read more on the website: php-di.org

Get community support in the Gitter chat room: Gitter chat

For Enterprise

Available as part of the Tidelift Subscription

The maintainers of php-di/php-di and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

去到顶部