ensure() public static method
Resolves the specified reference into the actual object and makes sure it is of the specified type.
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,
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 object ensure ( $reference, $type = null, $container = 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 | string |
The class/interface name to be checked. If null, type check will not be performed. |
$container | yii\di\ServiceLocator|yii\di\Container |
The container. This will be passed to get(). |
return | object |
The object referenced by the Instance, or |
throws | yii\base\InvalidConfigException |
if the reference is invalid |
Please login to continue.