Singletons

This chapter is largely obsolete in modern TYPO3 programming where services are used correctly: Stateless services should be "shared", meaning the service container creates a single instance and injects that instance into all other services that require it. These services are referred to as "singletons".

TYPO3 has an old way of designating a class as a singleton: Classes that implement SingletonInterface . This approach dates back to a time before TYPO3 offered a comprehensive dependency injection solution. The interface is still considered when injecting such a service and when creating an instance via GeneralUtility::makeInstance().

Example:

EXT:some_extension/Classes/MySingletonService.php
namespace Vendor\SomeExtension;

class MySingletonClass implements \TYPO3\CMS\Core\SingletonInterface
{
    // …
}
Copied!

SingletonInterface has no methods to implement. Services implementing the interface are automatically declared public.

Due to the overlap with "shared services", TYPO3 core development is gradually reducing the number of classes that implement SingletonInterface. This process often involves transforming service classes into stateless services, using dependency injection in service consumers, and updating tests to avoid reliance on the test-related method GeneralUtility::addSingletonInstance(). It is a gradual transition that requires time.

Extension developers should also refrain from using SingletonInterface: New code should not depend on it, and existing code be refactored to eliminate its usage over time.