PHP code example of lava / container
1. Go to this page and download the library: Download lava/container library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lava / container example snippets
php
ntainer = new \Lava\Container\Container();
php
class B{
protected $a;
function __construct(A $a)
{
$this->a = $a;
}
}
class A{
}
// 绑定类实例
$container->bind("a",new A);
// 传类名 他会自动帮你实例化A类
$container->bind("b","B");
// 使用闭包动态构建A类
$container->bind('a',function($container){
return new A;
});
//绑定自身
$container->bind("A");
//绑定一个共享实例
$container->singleton("A",function(){
return new A;
});
//绑定会返回一个实例构建器 你可以通过他来写别名或者覆盖旧的构建器
$container->bind("A")->setAlias("b")->setBuilder(function(){});