Final Class Yiisoft\Definitions\Reference
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Yiisoft\ |
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\ |
| resolve() | Yiisoft\ |
|
| to() | Yiisoft\ |
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\ |
If ID is not string. |
|---|---|---|
public static function optional(mixed $id): self
{
return new self($id, true);
}
| public mixed resolve ( \ | ||
| $container | \ |
|
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\ |
If ID is not string. |
|---|---|---|
public static function to(mixed $id): self
{
return new self($id, false);
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.