typo3registration

Is it better to use call_user_func() in (e.g.) tt_content.php TCA and why? (TYPO3)


I found in my ext_tables.php (build by Extension Builder) those rows:

<?php
defined('TYPO3_MODE') || die('Access denied.');

call_user_func(
    function()
    {
        \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addStaticFile('rm_navigation', 'Configuration/TypoScript', 'Navigation');
    }
);

I migrate this for TYPO3 8 to Configuration/TCA/Overrides/sys_template.php with those line:

<?php
defined('TYPO3_MODE') || die('Access denied.');

// Extensionregistration
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addStaticFile('rm_navigation', 'Configuration/TypoScript', 'Navigation');

I read that's better to use the call_user_func function for this, but he/she doesn't wrote why.

So: is it better to use the call_user_func function?

And: why is this better?

Especially for TYPO3.

Thanks.


Solution

  • There is exactly one purpose for this construct: encapsulation.

    Whenever you need a temporary variable in ext_tables.php or ext_localconf.php you risk leaking this variable into the same file of the next extension which is loaded after yours. A closure in PHP as a strict scope which prevents leaking variables into the closure as well as leaking variables to the surrounding code.

    The reason for this is that all ext_tables.php and ext_localconf.php files are concatenated to a single file and stored in the cache. This is no problem for TCA and TCA overrides since these are stored as whole after building the whole array.

    So in ext_tables.php or ext_localconf.php you should really use this construct.

    Bonus: in PHP 7.x and newer this can be simplified:

    (function() {
        // ...
    })();