phputil/di
Composer 安装命令:
composer require phputil/di
包简介
Dependency Injection for PHP
README 文档
README
Dependency Injection for PHP.
We use semantic version. See our releases.
Classes
Installation
composer require phputil/di
Example 1: automatic injection without configuring anything
class A {} class B {} class C { public $a, $b; function __construct( A $a, B $b ) { $this->a = $a; $this->b = $b; } } // Automatically creates "A" and "B", and inject them in "C" $c = DI::create( 'C' );
Example 2: configuring the injection for interfaces
interface MyInterface { function say( $what ); } class MyClass implements MyInterface { function say( $what ) { echo $what; } } // Configures "MyInterface" to be created using "MyClass" DI::config( DI::let( 'MyInterface' )->create( 'MyClass' ) ); $foo = DI::create( 'MyClass' ); // Create an instance of MyClass (no configuration required) $foo->say( 'hello' ); $bar = DI::create( 'MyInterface' ); // Create an instance of MyClass! $bar->say( 'world' );
Example 3: a more complex model
interface I {} class A {} class B implements I {} class C { public $i; function __construct( I $i ) { $this->i = $i; } } class X { public $a, $c; function __construct( A $a, C $c ) { $this->a = $a; $this->c = $c; } } // Configures "I" to be created using "B" DI::config( DI::let( 'I' )->create( 'B' ) ); // Automatically creates and injects "A" and "C", and // when creates "C", also injects "B" as "I". $x = DI::create( 'X' );
Example 4: passing constructor's arguments
class A { function __construct( $one, $two = 'world' ) { echo $one, ', ', $two; } } // Creates "A", passing constructor arguments as an array $a = DI::create( 'A', array( 'hello' ) ); // prints hello, world
Example 5: configuring shared instances
class A {} // Makes "A" a shared instance DI::config( DI::let( 'A' )->shared() ); $a1 = DI::create( 'A' ); $a2 = DI::create( 'A' ); var_dump( $a1 === $a2 ); // bool(true)
Example 6: configuring shared instances for interfaces
interface I {} class C implements I {} // Makes "I" a shared instance DI::config( DI::let( 'I' )->create( 'C' )->shared() ); $i1 = DI::create( 'I' ); $i2 = DI::create( 'I' ); var_dump( $i1 === $i2 ); // bool(true)
Example 7: defining a creation function
interface I {} class C implements I {} // Let you using a callable to create the desired instance DI::config( DI::let( 'I' )->call( function() { return new C(); } ) ); $i = DI::create( 'I' ); // Calls your function to create a "C" instance
Example 8: passing arguments for creation functions
class A { private $text; function __construct( $text ) { $this->text = $text; } } // Lets you customize a callable with parameters DI::config( DI::let( 'A' )->call( function( $a, $b ) { return new A( $a . $b ); } ) ); // Uses the customized constructor $a = DI::create( 'A', array( 'Hello, ', 'world' ) ); echo $a->text(); // Hello, world // Ignore the customized constructor passing true after the parameters $a2 = DI::create( 'A', array( 'Hi!' ), true ); echo $a2->text(); // Hi
phputil/di 适用场景与选型建议
phputil/di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 587 次下载、GitHub Stars 达 6, 最近一次更新时间为 2015 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dependency」 「injection」 「injector」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phputil/di 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phputil/di 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phputil/di 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
Inject dependencies into constructors and methods
The simplest [in]dependency injection container for PHP7, compatible with PSR-11.
An implementation of dependency injection (like RequireJS) in PHP.
Dependency injection container for the Monolith framework.
Http Microframework for Hack
统计信息
- 总下载量: 587
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: LGPL-3
- 更新时间: 2015-11-13