Dependency Injection

TYPO3 uses a Dependency Injection solution based on the corresponding PSR-11 compliant Symfony component to standardize object initialization throughout the core as well as in extensions.

The recommended way of injecting dependencies is to use constructor injection:

public function __construct(Dependency $dependency)
{
    $this->dependency = $dependency;
}

By default all classes shipped by the TYPO3 core system extensions are available for dependency injection.

Configure Dependency Injection in Extensions

Extensions have to configure their classes to make use of the dependency injection. This can be done in Configuration/Services.yaml. Alternatively Configuration/Services.php can be used.

# Configuration/Services.yaml
services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  Your\Namespace\:
    resource: '../Classes/*'

This is how a basic Services.yaml of an extension looks like. The meaning of autowire, autoconfigure and public will be explained below.

Note

Whenever service configuration or class dependencies change, the core cache needs to be flushed to rebuild the compiled Symfony container.

Autowire

autowire: true instructs the dependency injection component to calculate the required dependencies from type declarations. This works for constructor and inject methods. The calculation yields to a service initialization recipe which is cached in php code (in TYPO3 core cache).

Note

An extension doesn’t need to use autowiring, it is free to manually wire dependencies in the service configuration file.

Autoconfigure

It is suggested to enable autoconfigure: true as this will automatically add Symfony service tags based on implemented interfaces or base classes. For example autoconfiguration ensures that classes which implement \TYPO3\CMS\Core\SingletonInterface will be publicly available from the Symfony container.

Public

public: false is a performance optimization and is therefore suggested to be set in extensions. This settings controls which services are available through \Psr\Container\ContainerInterface->get(). However some classes, that need to be public, will be marked public automatically due to autoconfigure: true. These classes include Singletons, because they need to be shared with code that uses \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance() and Extbase controllers.

Knowing what to make public

Instances of \TYPO3\CMS\Core\SingletonInterface and Extbase controllers are marked public by default. Additionally some classes cannot be private as well. As the Symfony documentation puts it:

“Simply said: A service can be marked as private if you do not want to access it directly from your code.”

Official documentation for public and private services.

Direct access includes instantiation via \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(). This means every class that should make use of dependency injection and is not instantiated via injection itself but by e.g. \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance() have to be marked as public. Some examples for this are:

  • User Functions
  • None Extbase Controllers
  • Classes registered in Hooks

For such classes an extension can override the global public: false configuration in the Configuration/Services.yaml for each class.

# Configuration/Services.yaml
services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  Vendor\MyExtension\:
    resource: '../Classes/*'

  Vendor\MyExtension\UserFunction\ClassA:
    public: true

With this configuration you can use dependency injection in \Vendor\MyExtension\UserFunction\ClassA when it is created in the context of a USER TypoScript object which would not be possible if this class was private.

Errors resulting from wrong configuration

When objects using dependency injection are not configured properly, one or more of the following issues can be the result. In such a case, check whether the class has to be configured as public: true.

ArgumentCountError is raised on missing dependency injection for Constructor Injection:

(1/1) ArgumentCountError

Too few arguments to function Vendor\ExtName\Namespace\Class::__construct(),
0 passed in typo3/sysext/core/Classes/Utility/GeneralUtility.php on line 3461 and exactly 1 expected

An Error is raised on missing dependency injection for Method Injection, once the dependency is used within the code:

(1/1) Error

Call to a member function methodName() on null

Supported Ways of Dependency Injection

Classes should be adapted to avoid both, \TYPO3\CMS\Extbase\Object\ObjectManager and \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance() whenever possible. Class dependencies should be injected via constructor injection or setter methods.

Constructor Injection

A class dependency can simply be specified as a constructor argument:

public function __construct(Dependency $dependency)
{
    $this->dependency = $dependency;
}

Method Injection

As an alternative to constructor injection injectDependency() Methods can be used. Additionally a setDependency() will also work if it has the annotation @required:

/**
 * @param MyDependency $myDependency
 */
public function injectMyDependency(MyDependency $myDependency)
{
    $this->myDependency = $myDependency;
}

/**
 * @param MyOtherDependency $myOtherDependency
 * @required
 */
public function setMyOtherDependency(MyOtherDependency $myOtherDependency)
{
    $this->myOtherDependency = $myOtherDependency;
}

Interface Injection

It is possible to inject interfaces as well. If there is only one implementation for a certain interface the interface injection is simply resolved to this implementation:

public function __construct(DependencyInterface $dependency)
{
    $this->dependency = $dependency;
}

When multiple implementation of the same interface exist, an extension needs to specify which implementation should be injected when the interface is type hinted. Find out more about how this is achieved in the official Symfony documentation.

Further Information