# Container

> Container implements a [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) container.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [di](/docs/api/yii/di/index.md) › Container

| class | `yii\di\Container` |
| --- | --- |
| Extends | [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/di/Container.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/di/Container.php#L106) |

Container implements a [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) container.

A dependency injection (DI) container is an object that knows how to instantiate and configure objects and
all their dependent objects. For more information about DI, please refer to
[Martin Fowler's article](https://martinfowler.com/articles/injection.html).

Container supports constructor injection as well as property injection.

To use Container, you first need to set up the class dependencies by calling [`set()`](/docs/api/yii/di/container/index.md#method-set).
You then call [`get()`](/docs/api/yii/di/container/index.md#method-get) to create a new class object. The Container will automatically instantiate
dependent objects, inject them into the object being created, configure, and finally return the newly created object.

By default, [`Yii::$container`](/docs/api/yii/baseyii/index.md#property-container) refers to a Container instance which is used by [`Yii::createObject()`](/docs/api/yii/baseyii/index.md#method-createObject)
to create new object instances. You may use this method to replace the `new` operator
when creating a new object, which gives you the benefit of automatic dependency resolution and default
property configuration.

Below is an example of using Container:

```
namespace app\models;

use yii\base\BaseObject;
use yii\db\Connection;
use yii\di\Container;

interface UserFinderInterface
{
    function findUser();
}

class UserFinder extends BaseObject implements UserFinderInterface
{
    public $db;

    public function __construct(Connection $db, $config = [])
    {
        $this->db = $db;
        parent::__construct($config);
    }

    public function findUser()
    {
    }
}

class UserLister extends BaseObject
{
    public $finder;

    public function __construct(UserFinderInterface $finder, $config = [])
    {
        $this->finder = $finder;
        parent::__construct($config);
    }
}

$container = new Container;
$container->set('yii\db\Connection', [
    'dsn' => '...',
]);
$container->set('app\models\UserFinderInterface', [
    'class' => 'app\models\UserFinder',
]);
$container->set('userLister', 'app\models\UserLister');

$lister = $container->get('userLister');

// which is equivalent to:

$db = new \yii\db\Connection(['dsn' => '...']);
$finder = new UserFinder($db);
$lister = new UserLister($finder);
```

For more details and usage information on Container, see the [guide article on di-containers](/docs/guide/concept-di-container/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-definitions"></span>`$definitions` | `array` | The list of the object definitions or the loaded shared objects (type or ID => definition or instance). | `Container` |
| <span id="property-resolveArrays"></span>`$resolveArrays` write-only | `bool` | Whether to attempt to resolve elements in array dependencies. | `Container` |
| <span id="property-singleton"></span>`$singleton` write-only | `string` | Class name, interface name or alias name | `Container` |
| [`$singletons`](#property-singletons) write-only | `array` | Array of singleton definitions. | `Container` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`clear()`](#method-clear) | Removes the definition for the specified name. | `Container` |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`get()`](#method-get) | Returns an instance of the requested class. | `Container` |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`getDefinitions()`](#method-getDefinitions) | Returns the list of the object definitions or the loaded shared objects. | `Container` |
| [`has()`](#method-has) | Returns a value indicating whether the container has the definition of the specified name. | `Container` |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`hasSingleton()`](#method-hasSingleton) | Returns a value indicating whether the given name corresponds to a registered singleton. | `Container` |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`invoke()`](#method-invoke) | Invoke a callback with resolving dependencies in parameters. | `Container` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`resolveCallableDependencies()`](#method-resolveCallableDependencies) | Resolve dependencies for a function. | `Container` |
| [`set()`](#method-set) | Registers a class definition with this container. | `Container` |
| [`setDefinitions()`](#method-setDefinitions) | Registers class definitions within this container. | `Container` |
| [`setResolveArrays()`](#method-setResolveArrays) |  | `Container` |
| [`setSingleton()`](#method-setSingleton) | Registers a class definition with this container and marks the class as a singleton class. | `Container` |
| [`setSingletons()`](#method-setSingletons) | Registers class definitions as singletons within this container by calling [`setSingleton()`](/docs/api/yii/di/container/index.md#method-setSingleton). | `Container` |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`build()`](#method-build) protected | Creates an instance of the specified class. | `Container` |
| [`getDependencies()`](#method-getDependencies) protected | Returns the dependencies of the specified class. | `Container` |
| [`mergeParams()`](#method-mergeParams) protected | Merges the user-specified constructor parameters with the ones registered via [`set()`](/docs/api/yii/di/container/index.md#method-set). | `Container` |
| [`normalizeDefinition()`](#method-normalizeDefinition) protected | Normalizes the class definition. | `Container` |
| [`resolveDependencies()`](#method-resolveDependencies) protected | Resolves dependencies by replacing them with the actual object instances. | `Container` |

## Property details

### $singletons <span id="property-singletons"></span>

```php
public array $singletons
```

Array of singleton definitions.

See [`setDefinitions()`](/docs/api/yii/di/container/index.md#method-setDefinitions) for allowed
formats of array.

**Access**

Write-only (virtual property).

## Method details

### clear() <span id="method-clear"></span>

```php
public function clear(string $class): mixed
```

Removes the definition for the specified name.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$class` | `string` | Class name, interface name or alias name |

**Returns**

`mixed`

### get() <span id="method-get"></span>

```php
public function get(string|class-string<yii\di\T>|yii\di\Instance $class, array $params = [], array $config = []): ($class is class-string<yii\di\T> ? yii\di\T : object)
```

Returns an instance of the requested class.

You may provide constructor parameters (`$params`) and object configurations (`$config`)
that will be used during the creation of the instance.

If the class implements [`Configurable`](/docs/api/yii/base/configurable/index.md), the `$config` parameter will be passed as the last
parameter to the class constructor; Otherwise, the configuration will be applied *after* the object is
instantiated.

Note that if the class is declared to be singleton by calling [`setSingleton()`](/docs/api/yii/di/container/index.md#method-setSingleton),
the same instance of the class will be returned each time this method is called.
In this case, the constructor parameters and object configurations will be used
only if the class is instantiated the first time.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$class` | `string\|class-string<yii\di\T>\|`[`Instance`](/docs/api/yii/di/instance/index.md) | The class Instance, name, or an alias name (e.g. `foo`) that was previously registered via [`set()`](/docs/api/yii/di/container/index.md#method-set) or [`setSingleton()`](/docs/api/yii/di/container/index.md#method-setSingleton). |
| `$params` | `array` | A list of constructor parameter values. Use one of two definitions: - Parameters as name-value pairs, for example: `['posts' => PostRepository::class]`. - Parameters in the order they appear in the constructor declaration. If you want to skip some parameters, you should index the remaining ones with the integers that represent their positions in the constructor parameter list. Dependencies indexed by name and by position in the same array are not allowed. |
| `$config` | `array` | A list of name-value pairs that will be used to initialize the object properties. |

**Returns**

`($class is class-string<yii\di\T> ? yii\di\T : object)`: An instance of the requested class.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the class cannot be recognized or correspond to an invalid definition
- [`NotInstantiableException`](/docs/api/yii/di/notinstantiableexception/index.md): If resolved to an abstract class or an interface (since 2.0.9)

### getDefinitions() <span id="method-getDefinitions"></span>

```php
public function getDefinitions(): array
```

Returns the list of the object definitions or the loaded shared objects.

**Returns**

`array`: The list of the object definitions or the loaded shared objects (type or ID => definition or instance).

### has() <span id="method-has"></span>

```php
public function has(string $class): bool
```

Returns a value indicating whether the container has the definition of the specified name.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$class` | `string` | Class name, interface name or alias name |

**Returns**

`bool`: Whether the container has the definition of the specified name.

**See also**

- [`set()`](/docs/api/yii/di/container/index.md#method-set)

### hasSingleton() <span id="method-hasSingleton"></span>

```php
public function hasSingleton(string $class, bool $checkInstance = false): bool
```

Returns a value indicating whether the given name corresponds to a registered singleton.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$class` | `string` | Class name, interface name or alias name |
| `$checkInstance` | `bool` | Whether to check if the singleton has been instantiated. |

**Returns**

`bool`: Whether the given name corresponds to a registered singleton. If `$checkInstance` is true, the method should return a value indicating whether the singleton has been instantiated.

### invoke() <span id="method-invoke"></span>

```php
public function invoke(callable $callback, array $params = []): mixed
```

Invoke a callback with resolving dependencies in parameters.

This method allows invoking a callback and let type hinted parameter names to be
resolved as objects of the Container. It additionally allows calling function using named parameters.

For example, the following callback may be invoked using the Container to resolve the formatter dependency:

```
$formatString = function($string, \yii\i18n\Formatter $formatter) {
   // ...
}
Yii::$container->invoke($formatString, ['string' => 'Hello World!']);
```

This will pass the string `'Hello World!'` as the first param, and a formatter instance created
by the DI container as the second param to the callable.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$callback` | `callable` | Callable to be invoked. |
| `$params` | `array` | The array of parameters for the function. This can be either a list of parameters, or an associative array representing named function parameters. |

**Returns**

`mixed`: The callback return value.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if a dependency cannot be resolved or if a dependency cannot be fulfilled.
- [`NotInstantiableException`](/docs/api/yii/di/notinstantiableexception/index.md): If resolved to an abstract class or an interface (since 2.0.9)

**Since**

2.0.7

### resolveCallableDependencies() <span id="method-resolveCallableDependencies"></span>

```php
public function resolveCallableDependencies(callable $callback, array $params = []): array
```

Resolve dependencies for a function.

This method can be used to implement similar functionality as provided by [`invoke()`](/docs/api/yii/di/container/index.md#method-invoke) in other
components.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$callback` | `callable` | Callable to be invoked. |
| `$params` | `array` | The array of parameters for the function, can be either numeric or associative. |

**Returns**

`array`: The resolved dependencies.

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if a dependency cannot be resolved or if a dependency cannot be fulfilled.
- [`NotInstantiableException`](/docs/api/yii/di/notinstantiableexception/index.md): If resolved to an abstract class or an interface (since 2.0.9)

**Since**

2.0.7

### set() <span id="method-set"></span>

```php
public function set(string $class, mixed $definition = [], array $params = []): $this
```

Registers a class definition with this container.

For example,

```
// register a class name as is. This can be skipped.
$container->set('yii\db\Connection');

// register an interface
// When a class depends on the interface, the corresponding class
// will be instantiated as the dependent object
$container->set('yii\mail\MailInterface', 'yii\swiftmailer\Mailer');

// register an alias name. You can use $container->get('foo')
// to create an instance of Connection
$container->set('foo', 'yii\db\Connection');

// register a class with configuration. The configuration
// will be applied when the class is instantiated by get()
$container->set('yii\db\Connection', [
    'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8',
]);

// register an alias name with class configuration
// In this case, a "class" element is required to specify the class
$container->set('db', [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8',
]);

// register a PHP callable
// The callable will be executed when $container->get('db') is called
$container->set('db', function ($container, $params, $config) {
    return new \yii\db\Connection($config);
});
```

If a class definition with the same name already exists, it will be overwritten with the new one.
You may use [`has()`](/docs/api/yii/di/container/index.md#method-has) to check if a class definition already exists.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$class` | `string` | Class name, interface name or alias name |
| `$definition` | `mixed` | The definition associated with `$class`. It can be one of the following: - a PHP callable: The callable will be executed when [`get()`](/docs/api/yii/di/container/index.md#method-get) is invoked. The signature of the callable should be `function ($container, $params, $config)`, where `$params` stands for the list of constructor parameters, `$config` the object configuration, and `$container` the container object. The return value of the callable will be returned by [`get()`](/docs/api/yii/di/container/index.md#method-get) as the object instance requested. - a configuration array: the array contains name-value pairs that will be used to initialize the property values of the newly created object when [`get()`](/docs/api/yii/di/container/index.md#method-get) is called. The `class` element stands for the class of the object to be created. If `class` is not specified, `$class` will be used as the class name. - a string: a class name, an interface name or an alias name. |
| `$params` | `array` | The list of constructor parameters. The parameters will be passed to the class constructor when [`get()`](/docs/api/yii/di/container/index.md#method-get) is called. |

**Returns**

`$this`: The container itself

### setDefinitions() <span id="method-setDefinitions"></span>

```php
public function setDefinitions(array $definitions): mixed
```

Registers class definitions within this container.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$definitions` | `array` | Array of definitions. There are two allowed formats of array. The first format: - key: class name, interface name or alias name. The key will be passed to the [`set()`](/docs/api/yii/di/container/index.md#method-set) method as a first argument `$class`. - value: the definition associated with `$class`. Possible values are described in [`set()`](/docs/api/yii/di/container/index.md#method-set) documentation for the `$definition` parameter. Will be passed to the [`set()`](/docs/api/yii/di/container/index.md#method-set) method as the second argument `$definition`. Example: $container->setDefinitions([ 'yii\web\Request' => 'app\components\Request', 'yii\web\Response' => [ 'class' => 'app\components\Response', 'format' => 'json' ], 'foo\Bar' => function () { $qux = new Qux; $foo = new Foo($qux); return new Bar($foo); } ]); The second format: - key: class name, interface name or alias name. The key will be passed to the [`set()`](/docs/api/yii/di/container/index.md#method-set) method as a first argument `$class`. - value: array of two elements. The first element will be passed the [`set()`](/docs/api/yii/di/container/index.md#method-set) method as the second argument `$definition`, the second one — as `$params`. Example: $container->setDefinitions([ 'foo\Bar' => [ ['class' => 'app\Bar'], [Instance::of('baz')] ] ]); |

**Returns**

`mixed`

**Since**

2.0.11

**See also**

- [`set()`](/docs/api/yii/di/container/index.md#method-set) (to know more about possible values of definitions)

### setResolveArrays() <span id="method-setResolveArrays"></span>

```php
public function setResolveArrays(bool $value): mixed
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `bool` | Whether to attempt to resolve elements in array dependencies |

**Returns**

`mixed`

**Since**

2.0.37

### setSingleton() <span id="method-setSingleton"></span>

```php
public function setSingleton(string $class, mixed $definition = [], array $params = []): $this
```

Registers a class definition with this container and marks the class as a singleton class.

This method is similar to [`set()`](/docs/api/yii/di/container/index.md#method-set) except that classes registered via this method will only have one
instance. Each time [`get()`](/docs/api/yii/di/container/index.md#method-get) is called, the same instance of the specified class will be returned.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$class` | `string` | Class name, interface name or alias name |
| `$definition` | `mixed` | The definition associated with `$class`. See [`set()`](/docs/api/yii/di/container/index.md#method-set) for more details. |
| `$params` | `array` | The list of constructor parameters. The parameters will be passed to the class constructor when [`get()`](/docs/api/yii/di/container/index.md#method-get) is called. |

**Returns**

`$this`: The container itself

**See also**

- [`set()`](/docs/api/yii/di/container/index.md#method-set)

### setSingletons() <span id="method-setSingletons"></span>

```php
public function setSingletons(array $singletons): mixed
```

Registers class definitions as singletons within this container by calling [`setSingleton()`](/docs/api/yii/di/container/index.md#method-setSingleton).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$singletons` | `array` | Array of singleton definitions. See [`setDefinitions()`](/docs/api/yii/di/container/index.md#method-setDefinitions) for allowed formats of array. |

**Returns**

`mixed`

**Since**

2.0.11

**See also**

- [`setDefinitions()`](/docs/api/yii/di/container/index.md#method-setDefinitions) (for allowed formats of $singletons parameter)
- [`setSingleton()`](/docs/api/yii/di/container/index.md#method-setSingleton) (to know more about possible values of definitions)

### build() <span id="method-build"></span>

```php
protected function build(class-string<yii\di\T> $class, array $params, array $config): yii\di\T
```

Creates an instance of the specified class.

This method will resolve dependencies of the specified class, instantiate them, and inject
them into the new instance of the specified class.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$class` | `class-string<yii\di\T>` | The class name |
| `$params` | `array` | Constructor parameters |
| `$config` | `array` | Configurations to be applied to the new instance |

**Returns**

`yii\di\T`: The newly created instance of the specified class

**Throws**

- [`NotInstantiableException`](/docs/api/yii/di/notinstantiableexception/index.md): If resolved to an abstract class or an interface (since 2.0.9)

### getDependencies() <span id="method-getDependencies"></span>

```php
protected function getDependencies(class-string<yii\di\T> $class): array{ReflectionClass<yii\di\T>, array<string, mixed>}
```

Returns the dependencies of the specified class.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$class` | `class-string<yii\di\T>` | Class name, interface name or alias name |

**Returns**

`array{`[`ReflectionClass`](https://www.php.net/manual/en/class.reflectionclass.php)`<yii\di\T>, array<string, mixed>}`: The dependencies of the specified class.

**Throws**

- [`NotInstantiableException`](/docs/api/yii/di/notinstantiableexception/index.md): if a dependency cannot be resolved or if a dependency cannot be fulfilled.

### mergeParams() <span id="method-mergeParams"></span>

```php
protected function mergeParams(string $class, array $params): array
```

Merges the user-specified constructor parameters with the ones registered via [`set()`](/docs/api/yii/di/container/index.md#method-set).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$class` | `string` | Class name, interface name or alias name |
| `$params` | `array` | The constructor parameters |

**Returns**

`array`: The merged parameters

### normalizeDefinition() <span id="method-normalizeDefinition"></span>

```php
protected function normalizeDefinition(string $class, string|array|callable $definition): array
```

Normalizes the class definition.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$class` | `string` | Class name |
| `$definition` | `string\|array\|callable` | The class definition |

**Returns**

`array`: The normalized class definition

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if the definition is invalid.

### resolveDependencies() <span id="method-resolveDependencies"></span>

```php
protected function resolveDependencies(array $dependencies, ReflectionClass<object>|null $reflection = null): array
```

Resolves dependencies by replacing them with the actual object instances.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$dependencies` | `array` | The dependencies |
| `$reflection` | [`ReflectionClass`](https://www.php.net/manual/en/class.reflectionclass.php)`<object>\|null` | The class reflection associated with the dependencies |

**Returns**

`array`: The resolved dependencies

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if a dependency cannot be resolved or if a dependency cannot be fulfilled.