usmanhalalit/viocon
最新稳定版本:1.0.1
Composer 安装命令:
composer require usmanhalalit/viocon
包简介
A simple and flexible Dependency Injection container for PHP.
关键字:
README 文档
README
A simple and flexible Dependency Injection or Service container for PHP.
It can be extremely helpful to decouple your dependencies. You can also mock dependencies at runtime.
Installation
Viocon uses Composer to make things easy.
Learn to use composer and add this to require (in your composer.json):
"usmanhalalit/viocon": "1.0.*@dev"
Library on Packagist.
Usage
$container = new \Viocon\Container();
You can optionally create a class alias too
new \Viocon\Container('Container');
Viocon will create a class alias for you with the given name 'Container'. Now you can use this class' methods statically, like
\Container::set(...); \Container::build(...);
And so ...
Bind a closure
$container->set('myClosure', function ($test1, $test2) { $stdClass = new \stdClass(); $stdClass->testVar1 = $test1; $stdClass->testVar2 = $test2; $stdClass->testMethod = function ($test3) { return $test3; }; return $stdClass; }); $object = $container->build('myClosure', array('Test Var 1', 'Test Var 2')) echo $object->testVar1;
Build Any Class and Optionally Pass Parameters
$container->build('\PDO', array('myConnectionInfo'));
Singleton
$container->set('mySingleton', '\\stdClass'); $stdClass = $container->build('mySingleton');
Replacing Objects at Runtime
It can be useful while testing with mocked object
$mockedClass = new \stdClass(); $mockedClass->testVar = 'mocked'; static::$container->setInstance('\\stdClass', $mockedClass); $stdClass = static::$container->build('\\stdClass'); $this->assertEquals('mocked', $stdClass->testVar);
统计信息
- 总下载量: 2.25M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 1
- 依赖项目数: 9
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04