Final Class Yiisoft\Factory\Factory
| Inheritance | Yiisoft\ |
|---|
Factory allows creating objects passing arguments runtime.
A factory will try to use a PSR-11 compliant container to get dependencies, but will fall back to manual instantiation if the container cannot provide a required dependency.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| create() | Creates a new object using the given configuration. | Yiisoft\ |
| withDefinitions() | Yiisoft\ |
Method Details
| public mixed __construct ( \ | ||
| $container | \ |
Container to use for resolving dependencies. |
| $definitions | array |
Definitions to create objects with. |
| $validate | boolean |
If definitions should be validated when set. |
| throws | \ |
|
|---|---|---|
public function __construct(
?ContainerInterface $container = null,
array $definitions = [],
private bool $validate = true,
) {
$this->validateDefinitions($definitions);
$this->internalContainer = new FactoryInternalContainer($container, $definitions);
}
Creates a new object using the given configuration.
You may view this method as an enhanced version of the new operator.
The method supports creating an object based on a class name, a configuration array or
an anonymous function.
Below are some usage examples:
// create an object using a class name
$object = $factory->create(\Yiisoft\Db\Connection::class);
// create an object using a configuration array
$object = $factory->create([
'class' => \Yiisoft\Db\Connection\Connection::class,
'__construct()' => [
'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
],
'setUsername()' => ['root'],
'setPassword()' => [''],
'setCharset()' => ['utf8'],
]);
Using Container, this method can also identify dependent objects, instantiate them and inject them into the newly created object.
| public mixed|object create ( mixed $config ) | ||
| $config | mixed |
The object configuration. This can be specified in one of the following forms:
|
| return | mixed|object |
The created object. |
|---|---|---|
| throws | \ |
If the configuration is invalid. |
| throws | \ |
|
| throws | Yiisoft\ |
|
| throws | \ |
|
public function create(mixed $config): mixed
{
if ($this->validate) {
DefinitionValidator::validate($config);
}
if (is_string($config)) {
if ($this->internalContainer->hasDefinition($config)) {
$definition = $this->internalContainer->getDefinition($config);
} elseif (class_exists($config)) {
$definition = ArrayDefinition::fromPreparedData($config);
} else {
throw new NotFoundException($config);
}
} else {
$definition = $this->createDefinition($config);
}
return $this->internalContainer->create($definition);
}
| public self withDefinitions ( array | ||
| $definitions | array |
Definitions to create objects with. |
| throws | \ |
|
|---|---|---|
public function withDefinitions(array $definitions): self
{
$this->validateDefinitions($definitions);
$new = clone $this;
$new->internalContainer = $this->internalContainer->withDefinitions($definitions);
return $new;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.