Final Class Yiisoft\Definitions\Reference
| Inheritance | Yiisoft\Definitions\Reference |
|---|---|
| Implements | Yiisoft\Definitions\Contract\ReferenceInterface |
The Reference defines a dependency to a service in the container or factory in another service definition.
For example:
[
InterfaceA::class => ConcreteA::class,
'alternativeForA' => ConcreteB::class,
MyService::class => [
'__construct()' => [
Reference::to('alternativeForA'),
],
],
]
Public Methods
| Method | Description | Defined By |
|---|---|---|
| optional() | Optional reference returns null when there is no corresponding definition in container. |
Yiisoft\Definitions\Reference |
| resolve() | Yiisoft\Definitions\Reference | |
| to() | Yiisoft\Definitions\Reference |
Method Details
Optional reference returns null when there is no corresponding definition in container.
| public static self optional ( mixed $id ) | ||
| $id | mixed |
ID of the service or object to point to. |
| throws | Yiisoft\Definitions\Exception\InvalidConfigException |
If ID is not string. |
|---|---|---|
public static function optional(mixed $id): self
{
return new self($id, true);
}
| public mixed resolve ( \Psr\Container\ContainerInterface $container ) | ||
| $container | \Psr\Container\ContainerInterface | |
public function resolve(ContainerInterface $container): mixed
{
return (!$this->optional || $container->has($this->id)) ? $container->get($this->id) : null;
}
| public static self to ( mixed $id ) | ||
| $id | mixed | |
| throws | Yiisoft\Definitions\Exception\InvalidConfigException |
If ID is not string. |
|---|---|---|
public static function to(mixed $id): self
{
return new self($id, false);
}
Signup or Login in order to comment.