在 TYPO3 的 ext_localconf.php 中使用“使用”?(致命错误:无法使用...作为...因为名称已在使用中)

Syb*_*ers 3 typo3

因为这个,我刚刚有一个错误:

Fatal error: Cannot use TYPO3\CMS\Core\Utility\GeneralUtility
as GeneralUtility because the name is already in use
in /var/www/mysite/public/typo3temp/var/cache/code/cache_core/ext_localconf_feb178af00fe22e00dc62d7dcd6d4d16f5d4fc3a.php
on line 4508
Run Code Online (Sandbox Code Playgroud)

违规行有:

use TYPO3\CMS\Core\Utility\GeneralUtility;
Run Code Online (Sandbox Code Playgroud)

显然,某些扩展ext_localconf.php在我的系统上使用了“use” 。由于所有这些都连接到缓存中的一个文件中,因此可能会导致多个 use 语句。

是否还有某种方法可以使用“使用”而不会导致这些错误?推荐的方式是什么?

我检查了文档:

您绝不能在 files 全局范围内使用 use 语句 - 这会使缓存的脚本概念中断并可能与其他扩展名冲突。

https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ExtensionArchitecture/ConfigurationFiles/Index.html

在推荐的闭包函数中使用“use”,例如:

(function () {
    // Add your code here
    use TYPO3\CMS\Core\Utility\GeneralUtility;
    // ...
})();
Run Code Online (Sandbox Code Playgroud)

导致我的系统出现语法错误。

专家们有什么建议?


顺便提一句。该领域中有多个扩展在 ext_localconf.php 的全局范围内具有 use 语句。如果我没记错的话,这可能会严重破坏。

Rud*_*dde 9

use用于在 PHP 中导入/别名命名空间只能在全局范围内使用,不能在块范围内使用。请参阅https://www.php.net/manual/en/language.namespaces.importing.php#language.namespaces.importing.scope。所以你根本不应该使用useinext_localconf.php而是使用完整的命名空间。

如果您确实使用use了某些扩展,则ext_localconf.php应该让该扩展的维护者知道这是错误的,并且可能(并且将会)导致致命错误。