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