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-18 12:22:04
最后一次提交2024-04-18 13:29:02
星數2.6k
關注者數82
派生數311
提交數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.

去到頂部