The Zend\Di\Injector is responsible for creating instances by providing the dependencies required by the class.

The dependencies are resolved by analyzing the constructor parameters of the requested class via reflection. For parameters defined with a class or interface typehint, the configured preferences are taken into account.

A Zend\Di\ConfigInterface can be provided to configure the injector. See the Configuration chapter for details.

Create instances

Instances can be created by calling create():

use Zend\Di\Injector;

$injector = new Injector()

Create instances with parameters

You can also pass construction parameters when calling create:

$injector->create(MyDbAdapter::class, [
    'username' => 'johndoe',

Parameters passed to create() will overwrite any configured injection for the requested class.

Generally the following behavior applies for parameter values that are not ValueInjection or TypeInjection instances:


// Assume the following classes
class Foo

class SpecialFoo extends Foo

class Bar
    public function __construct(Foo $foo, $type = null)

// Usage
use Zend\Di\Resolver\ValueInjection;
use Zend\Di\Resolver\TypeInjection;

// Creates Bar with an instance of SpecialFoo from the IoC container:
$injector->create(Bar::class, [
    'foo' => SpecialFoo::class,

// Creates Bar with the given instance of SpecialFoo bypassing the IoC
// container:
$injector->create(Bar::class, [
    'foo' => ValueInjection(new SpecialFoo()),

// Creates Bar with an instance of Foo and the string literal 'SpecialFoo' for
// $type:
$injector->create(Bar::class, [
    'type' => SpecialFoo::class,

// Creates Bar with an instance of Foo and an instance of SpecialFoo from the
// IoC container for $type:
$injector->create(Bar::class, [
    'type' => new TypeInjection(SpecialFoo::class),

Refer to the Parameters section in the Configuration chapter for all possibilities of how parameters can be declared.

Check if a type is creatable

If you uncertain whether or not the injector can create a specific type, you can test it with the canCreate() method.

For example, if you were to consume the class name in a generic service factory for zend-servicemanager:

use Zend\Di\Injector;

/** @var \Zend\ServiceManager\ServiceManager $container */
$factory = function($container, $requestedName, array $options = null) {
    $injector = $container->get(Injector::class);

    if (! $injector->canCreate($requestedName)) {
        throw new \RuntimeException('Bad service name');

    return $injector->create($requestedName, $options? : []);

$serviceManager->setFactory('Foo', $factory);
$serviceManager->setFactory('Bar', $factory);
$serviceManager->setFactory(stdClass::class, $factory);