本文整理汇总了PHP中Container::build方法的典型用法代码示例。如果您正苦于以下问题:PHP Container::build方法的具体用法?PHP Container::build怎么用?PHP Container::build使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Container
的用法示例。
在下文中一共展示了Container::build方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testSingletonWithPersistence
public function testSingletonWithPersistence()
{
$container = new Container();
$container->singleton('mySingleton', '\\stdClass');
$stdClass = $container->build('mySingleton');
$stdClass->testVar = 'value';
$stdClass2 = $container->build('mySingleton');
$this->assertEquals('value', $stdClass2->testVar);
}
示例2: __invoke
/**
* Magic method. Runs when using this class as a function.
* Ex: $object = new Definition($container, $class);
* $invoked = $object();
*
* @return object The instantiated $class with optional args passed to the constructor and methods called.
*/
public function __invoke()
{
$this->mergeInheritedDependencies();
if (empty($this->arguments)) {
$object = $this->container->build($this->class);
} else {
$reflection = new \ReflectionClass($this->class);
$arguments = [];
foreach ($this->arguments as $arg) {
if (is_string($arg) && (class_exists($arg) || $this->container->bound($arg))) {
$arguments[] = $this->container->resolve($arg);
continue;
}
$arguments[] = $arg;
}
$object = $reflection->newInstanceArgs($arguments);
}
if ($object instanceof ContainerAwareInterface) {
$this->withMethod('setContainer', [$this->container]);
}
return $this->callMethods($object);
}
示例3: __invoke
/**
* Magic method. Runs when using this class as a function.
* Ex: $object = new Definition($container, $class);
* $invoked = $object();
*
* @return object The instantiated $class with optional args passed to the constructor and methods called.
*/
public function __invoke()
{
if (empty($this->arguments)) {
if (empty($this->methods)) {
$object = $this->container->build($this->class);
} else {
$object = new $this->class();
}
} else {
$reflection = new \ReflectionClass($this->class);
$arguments = array();
foreach ($this->arguments as $arg) {
if (is_string($arg) && (class_exists($arg) || $this->container->bound($arg))) {
$arguments[] = $this->container->resolve($arg);
continue;
}
$arguments[] = $arg;
}
$object = $reflection->newInstanceArgs($arguments);
}
return $this->callMethods($object);
}