The reference may be specified as a string or an Instance object. If the former,
it will be treated as a component ID, a class/interface name or an alias, depending on the container type.
If you do not specify a container, the method will first try Yii::$app followed by Yii::$container.
For example,
php
use yii\db\Connection;
returns Yii::$app->db
$db = Instance::ensure('db', Connection::className());
returns an instance of Connection using the given configuration
$db = Instance::ensure(['dsn' => 'sqlite:path/to/my.db'], Connection::className());
public static ensure ( object | string | array | static $reference, string $type = null, |
||
$reference | object | string | array | static | an object or a reference to the desired object. You may specify a reference in terms of a component ID or an Instance object. Starting from version 2.0.2, you may also pass in a configuration array for creating the object. If the "class" value is not specified in the configuration array, it will use the value of `$type`. |
$type | string | the class/interface name to be checked. If null, type check will not be performed. |
$container | the container. This will be passed to [[get()]]. | |
리턴 | object | the object referenced by the Instance, or `$reference` itself if it is an object. |