You may use Instance::get to obtain the actual object referenced by [[id]].
Instance is mainly used in two places:
- When configuring a dependency injection container, you use Instance to reference a class name, interface name
or alias name. The reference can later be resolved into the actual object by the container.
- In classes which use service locator to obtain dependent objects.
The following example shows how to configure a DI container with Instance:
php
$container = new \yii\di\Container;
$container->set('cache', [
'class' => 'yii\caching\DbCache',
'db' => Instance::of('db')
]);
$container->set('db', [
'class' => 'yii\db\Connection',
'dsn' => 'sqlite:path/to/file.db',
]);
And the following example shows how a class retrieves a component from a service locator:
php
class DbCache extends Cache
{
public $db = 'db';
public function init()
{
parent::init();
$this->db = Instance::ensure($this->db, 'yii\db\Connection');
}
}