本文整理汇总了PHP中Container::share方法的典型用法代码示例。如果您正苦于以下问题:PHP Container::share方法的具体用法?PHP Container::share怎么用?PHP Container::share使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Container
的用法示例。
在下文中一共展示了Container::share方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testContainerToCallback
public function testContainerToCallback()
{
$c = new Container();
$c->share('service', function (Container $container) {
return $container;
});
$this->assertInstanceOf(Container::class, $c->get('service'));
}
示例2: testShare
public function testShare()
{
$this->object['obj'] = $this->object->share(function () {
return new \stdClass();
});
$this->assertInstanceOf('\\stdClass', $this->object['obj']);
$obj1 = $this->object['obj'];
$obj2 = $this->object['obj'];
$this->assertTrue($obj2 === $obj1);
}
示例3: testComplex
public function testComplex()
{
$c = new Container();
$c->share('A', function (Container $c) {
return new A($c->get('B'), $c->get('C'));
});
$c->share('B', function (Container $c) {
return new B($c->get('D'));
});
$c->share('C', function () {
return new C();
});
$c->share('D', function () {
return new D();
});
/** @var A $a */
$a = $c->get('A');
$this->assertEquals('ok', $a->x());
}
示例4: testExtendSharedServiceAfterConstruct
/**
* @expectedException UnexpectedValueException
* @expectedExceptionMessage Item "sharedService" is not a closure or invokable object and cannot be extended
*/
public function testExtendSharedServiceAfterConstruct()
{
$container = new Container();
$container->share('sharedService', function ($container) {
return new \stdClass();
});
$service = $container->get('sharedService');
$container->extend('sharedService', function ($container) {
$container->get('sharedService')->member = 'value';
});
}
示例5: testGetShared
public function testGetShared()
{
$container = new Container();
$container->share('service', function () {
return new \stdClass();
});
$service1 = $container->service;
$service2 = $container->service;
$this->assertInstanceOf('stdClass', $service1);
$this->assertInstanceOf('stdClass', $service2);
$this->assertSame($service2, $service1);
}