2.3.3. Interface Definitions Implementation Class

You can indicate the implementation class to use for an interface typehint with the class() method:


Setting the class() will cause the Container to use the definition for that other class. In the above example, that means the Foo initial and extended construction logic will be used for each FooInterface typehint. Factory Callable

Instead of indicating an implemention, you can set a callable factory for the interface definition. This lets you create the implementation object yourself, instead of letting the Container instantiate it for you.

The callable factory must have the following signature ...

function (Container $container) : object

... although the object typehint may be more specific if you like.

For example:

    ->factory(function (Container $container) : Foo {
        return new Foo(); // or perform any other complex creation logic