The autoloader takes care of finding classes in TYPO3. It is closely related to
\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance() which takes care of singleton
and XCLASS handling.
As a developer you should always instantiate classes either through
\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance() or with the Extbase
(which internally uses
Since TYPO3 CMS 6.0 and the introduction of namespaces, developers are strongly encouraged to use the namespaces. When using namespaces it is not necessary to explicitely declare classes in an autoloader files. All namespaced classes are automatically autoloaded.
Please refer to the Namespaces chapter for more information.
Naming convention or autoloader file¶
In TYPO3 every class must reside in its own file, i.e. there should be only
one class per PHP file. Extensions must not use
to load class files, but instead use the TYPO3 core API to automatically require a file
upon request of the class.
A developer has two options to help the core find a specific class:
- Use the class naming convention and file location.
- Register a class name together with its location in an
If it’s not possible to stick to the class naming and file location conventions - for whatever
reason - or if you don’t want to use namespaces, you can add a file to your extension called
ext_autoload.php, in the base directory. Its goal is to inform the autoloader about the
location of each class files. The autoloader automatically searches for this file when a class is
ext_autoload.php file must simply return a one-dimensional array
with the class name as key, and the file location as value. No other code is allowed in this file.
Examples for non-namespaced classes¶
The examples below are related to non-namespaced classes. When using namespaces, autoloading will happen without any extra effort on your part.
Consider the following:
- Extension name:
- Extension location:
- Class name:
- Required file location:
which respects the following rules:
- The class name must start with
- In the extension name underscores are converted to upper camel case, hence
- Every underscore after the extension name in the class name is resolved to a uppercases folder name
- The last part of the class name resolves to the file name with suffix
For a file which doesn’t follow any particular conventions, an entry must be
created in the extension’s
Example taken from an oldish version of extension “news”:
<?php $extensionClassesPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('news') . 'Classes/'; $default = array( 'tx_news_domain_model_dto_emconfiguration' => $extensionClassesPath . 'Domain/Model/Dto/EmConfiguration.php', 'tx_news_hooks_suggestreceiver' => $extensionClassesPath . 'Hooks/SuggestReceiver.php', 'tx_news_hooks_suggestreceivercall' => $extensionClassesPath . 'Hooks/SuggestReceiverCall.php', 'tx_news_utility_compatibility' => $extensionClassesPath . 'Utility/Compatibility.php', 'tx_news_utility_importjob' => $extensionClassesPath . 'Utility/ImportJob.php', 'tx_news_utility_emconfiguration' => $extensionClassesPath . 'Utility/EmConfiguration.php', 'tx_news_service_cacheservice' => $extensionClassesPath . 'Service/CacheService.php', ); return $default; ?>
The class names used as keys in the array must be in lower case, until TYPO3 4.7. This limitation was removed in TYPO3 6.0.