gpt4 book ai didi

typo3 - 在(例如)tt_content.php TCA 中使用 call_user_func() 会更好吗?为什么? (TYPO3)

转载 作者:行者123 更新时间:2023-12-05 00:48:36 28 4
gpt4 key购买 nike

我在我的 ext_tables.php(由 Extension Builder 构建)中找到了这些行:

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

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

我将 TYPO3 8 的这个迁移到 Configuration/TCA/Overrides/sys_template.php 与这些行:

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

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

我读到最好使用 call_user_func 函数,但他/她没有写为什么。

那么:使用 call_user_func 函数更好吗?

还有:为什么这样更好?

尤其是 TYPO3。

谢谢。

最佳答案

这种结构只有一个目的:封装。

当您在 ext_tables.phpext_localconf.php 中需要一个临时变量时,您就有可能将此变量泄漏到下一个扩展名的同一文件中,该文件在您之后加载。 PHP 中的闭包作为严格范围,可防止将变量泄漏到闭包中以及将变量泄漏到周围的代码中。

这样做的原因是所有 ext_tables.phpext_localconf.php 文件都连接成一个文件并存储在缓存中。这对于 TCA 和 TCA 覆盖没有问题,因为它们在构建整个数组后作为整体存储。

所以在 ext_tables.phpext_localconf.php 你真的应该使用这个结构。

奖励:在 PHP 7.x 和更新版本中,这可以简化:

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

关于typo3 - 在(例如)tt_content.php TCA 中使用 call_user_func() 会更好吗?为什么? (TYPO3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49514481/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com