本文整理匯總了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);
}