PHP Namespaces ¶
Since version 6.0, TYPO3 CMS uses PHP namespaces for all classes in the Core.
The general structure of namespaces is the following:
For the Core, the
to a system extension.
All classes must be located inside the
folder at the root of the
(system) extension. The
may contain several segments that correspond
to the path inside the
is the same as the corresponding file name, without the
“UpperCamelCase” is used for all segments.
See the chapter about ‘ClassAliasMap.php’ in the 6.2 documentation. . It may help you with migrating code from old to new conventions.
Core Example ¶
The good old
class has been renamed to:
This means that the class is now found in the “core” system extension, in folder
, in a file named
Usage in Extensions ¶
Extension developers are free to use their own vendor name. Important: It may consist of one segment only. Vendor names must start with an uppercase character and are usually written in UpperCamelCase style. In order to avoid problems with different filesystems, only the characters a-z, A-Z, 0-9 and the dash sign “-” are allowed for package names – don’t use special characters:
// good vendor name: \Webcompany // wrong vendor name: \Web\Company
The vendor name
is reserved and may not be used by extensions!
The package name corresponds to the extension key. Underscores in the extension key are removed in the namespace and replaced by upper camel-case. So extension key:
in the namespace.
As mentioned above, all classes
be located in the
your extension. All sub-folders translate to a segment of the category name and the class
name is the file name without the
Looking at the “examples” extension, class:
corresponds to namespace:
Inside the class, the namespace is declared as:
<?php namespace Documentation\Examples\Controller;
Namespaces in Extbase ¶
When registering components in Extbase, the vendor name must be used on top of the extension key.
For a backend module:
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerModule( '<vendorName>.<ExtensionName>', // ... );
For a frontend module:
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin( '<vendorName>.<ExtensionName>', // ... );
- Do not forget the dot after the vendor name.
- Do not use dots inside the vendor name.
Namespaces for Test Classes ¶
As for ordinary classes, namespaces for test classes start with a vendor name followed by the extension key.
All test classes reside in a
folder and thus the third segment
of the namespace must be “Tests”. Unit tests are located in a
which is the fourth segment of the namespace. Any further subfolders will
be subsequent segments.
So a test class in
will have as namespace
Creating Instances ¶
The following example shows how you can create instances by means of
$contentObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
to make the code more readable:
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
include and required ¶
There is no need for
classes adhering to namespace conventions will automatically be located and
included by the autoloader.