Typo3 CMS hooks and namespaced classes
This blog post is older than 4 years and may no longer be valid.
1 min read

Typo3 CMS hooks and namespaced classes


During work on my extension cwenvbanner to make it compatible with Typo3 CMS 7.2, I came across a good old friend, the hooks array $GLOBALS[‘TYPO3_CONF_VARS’][‘SC_OPTIONS’]. Since I sometimes struggle with the way how to declare the class and function to be called, I decided to write it down here - once and for all :-)

In short, this is how a hook can be defined

EXT:name_of_extension/path_and_filename_of_class:{&}classname->functionName

The ampersand before the class name is optional. If you add it, the class will be instantiated using the Singleton pattern.

Let’s have some examples. Firstly, let’s have a look at the old version I had in my extension:

$GLOBALS\['TYPO3\_CONF\_VARS'\]\['SC\_OPTIONS'\]\['tslib/class.tslib\_fe.php'\]\['contentPostProc-output'\]\['tx\_cwenvbanner'\] = 'EXT:cwenvbanner/class.tx\_cwenvbanner.php:&tx\_cwenvbanner->contentPostProc\_output';

For the new Typo3 CMS 7 compatible version of cwenvbanner I wanted to use a namespaced classes for the heck of it. I came up with the inspiring name “Main” which now resides in the Classes directory of the extension, so need I changed the path and filename accordingly. I also need to provide the fully qualified class name, i.e. Cw\Cwenvbanner\Main.

This is how it looks like now:

$GLOBALS\['TYPO3\_CONF\_VARS'\]\['SC\_OPTIONS'\]\['tslib/class.tslib\_fe.php'\]\['contentPostProc-output'\]\['tx\_cwenvbanner'\] = 'EXT:cwenvbanner/Classes/Main.php:&Cw\\\\Cwenvbanner\\\\Main->contentPostProc\_output';