本文整理汇总了PHP中Container::bind方法的典型用法代码示例。如果您正苦于以下问题:PHP Container::bind方法的具体用法?PHP Container::bind怎么用?PHP Container::bind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Container
的用法示例。
在下文中一共展示了Container::bind方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testCanBindInterfaceToImplementationUsingBind
public function testCanBindInterfaceToImplementationUsingBind()
{
$this->container->bind('NetRivet\\Container\\Test\\BarInterface', 'NetRivet\\Container\\Test\\Bar1');
$foo = $this->container->make('NetRivet\\Container\\Test\\Foo');
$bar = $foo->getBar();
$this->assertInstanceOf('NetRivet\\Container\\Test\\Bar1', $bar);
}
示例2: setUp
/**
* Setup the tests
*/
public function setUp()
{
$this->app = IlluminageServiceProvider::make();
$this->app->instance('path.public', 'tests/public');
// Bind mocked config
$this->app->bind('config', function () {
return Mockery::mock('config', function ($mock) {
$mock->shouldReceive('get')->with('illuminage::image_engine', '')->andReturn('Gd');
$mock->shouldReceive('get')->with('illuminage::quality', '')->andReturn(75);
$mock->shouldReceive('get')->with('illuminage::cache_folder', '')->andReturn('');
});
});
// Create some dummy instances
$this->image = $this->app['illuminage']->image('foo.jpg');
$this->thumb = $this->app['illuminage']->thumb('foo.jpg', 100, 100);
}
示例3: bind
class Container
{
protected $binds;
protected $instances;
public function bind($abstract, $concrete)
{
if ($concrete instanceof Closure) {
$this->binds[$abstract] = $concrete;
} else {
$this->instances[$abstract] = $concrete;
}
}
public function make($abstract, $parameters = [])
{
if (isset($this->instances[$abstract])) {
return $this->instances[$abstract];
}
array_unshift($parameters, $this);
return call_user_func_array($this->binds[$abstract], $parameters);
}
}
$container = new Container();
$container->bind('superman', function ($container, $moduleName) {
return new Superman($container->make($moduleName));
});
$container->bind('xpower', function ($container) {
return new Xpower();
});
$superman_1 = $container->make('superman', ['xpower']);
var_dump($superman_1);
var_dump($superman_1 instanceof Superman);
示例4: testBindReadOnly
/**
* @covers ::bind
* @expectedException RuntimeException
*/
public function testBindReadOnly()
{
$this->container->setReadOnly(true);
$this->container->bind('something', $value);
}
示例5: Container
<?php
namespace RomanNumerals;
error_reporting(E_ALL);
ini_set('display_errors', 1);
require __DIR__ . '/vendor/autoload.php';
$routes = [['GET', '/api/generate/{arabicNumber:\\w+}', 'RomanNumerals\\ConvertController@generate'], ['GET', '/api/parse/{romanNumber:\\w+}', 'RomanNumerals\\ConvertController@parse']];
$container = new Container();
$container->bind('RomanNumerals\\ConvertController', function () {
$object = new ConvertController();
$object->setRomanNumeralGenerator(new RomanNumeralGenerator());
return $object;
});
$container->bind('RomanNumerals\\Controller', function () {
return new ConvertController();
});
$app = new Application($container, new Router($routes));
$app->start();
示例6: make
}
}
public function make($abstract, $parameters = [])
{
if (isset($this->instances[$abstract])) {
return $this->instances[$abstract];
}
array_unshift($parameters, $this);
return call_user_func_array($this->binds[$abstract], $parameters);
}
}
// 创建一个容器(后面称作超级工厂)
$container = new Container();
// 向该 超级工厂添加超人的生产脚本
$container->bind('superman', function ($container, $moduleName) {
return new Superman($container->make($moduleName));
});
// 向该 超级工厂添加超能力模组的生产脚本
$container->bind('xpower', function ($container) {
return new XPower();
});
// 同上
$container->bind('ultrabomb', function ($container) {
return new UltraBomb();
});
// echo '<pre>' . print_r($container, true) . '</pre>';# test
// ****************** 华丽丽的分割线 **********************
// 开始启动生产
$superman_1 = $container->make('superman', ['xpower']);
// $superman_2 = $container->make('superman', 'ultrabomb');
$superman_3 = $container->make('superman', ['xpower']);