Attention
TYPO3 v9 has reached its end-of-life September 30th, 2021 and is not maintained by the community anymore. Looking for a stable version? Use the version switch on the top left.
You can order Extended Long Term Support (ELTS) here: TYPO3 ELTS.
Singletons¶
TYPO3 supports the singleton patterns for classes. Singletons are
instantiated only once per request regardless of the number of
calls to GeneralUtility::makeInstance()
. To use a singleton
pattern, a class must implement the SingletonInterface
:
namespace Vendor\MyNamespace;
class MySingletonClass implements \TYPO3\CMS\Core\SingletonInterface
{
…
}
This interface has no specific methods to implement, but if implemented only one instance of the class will be created through given request.
Be aware that singletons are often considered as "anti pattern" by code architects and should be used with care - use them only if there are very good reasons to do so.