所以基本上我想创建多个 PrestaShop 模块。有时会在同一家商店中激活几个模块。问题是我想在两个模块中使用不同定义的类“MyTools”。
那会导致类重定义错误。我的解决方案是使用命名空间:
<?php
require_once('classes/MyTools.php');
use Module1\MyTools;
class Module1 extends Module
{
另一个模块:
<?php
require_once('classes/MyTools.php');
use Module2\MyTools;
class Module2 extends Module
{
MyTools 类在这里有所不同:仅保留使用的功能,某些功能可能会更新。所以我不能对所有模块使用相同的类,这也是不可能的。
现在这段代码运行良好,除了'
use
' 语句不能在使用 eval() 时使用;这正是我的问题,因为 PrestaShop 在重建和检查模块索引和更改时使用 eval():// If class does not exists, we include the file
if (!class_exists($module, false))
{
// Get content from php file
$filepath = _PS_MODULE_DIR_.$module.'/'.$module.'.php';
$file = trim(file_get_contents(_PS_MODULE_DIR_.$module.'/'.$module.'.php'));
if (substr($file, 0, 5) == '<?php')
$file = substr($file, 5);
if (substr($file, -2) == '?>')
$file = substr($file, 0, -2);
// If (false) is a trick to not load the class with "eval".
// This way require_once will works correctly
if (eval('if (false){ '.$file.' }') !== false)
require_once( _PS_MODULE_DIR_.$module.'/'.$module.'.php' );
else
$errors[] = '...';
}
有没有什么方法可以绕过这个语句而不会出现 T_USE 的解析错误?
if (eval('if (false){ '.$file.' }') !== false)
错误
Parse error: syntax error, unexpected 'use' (T_USE) in in \classes\module\Module.php(1096) : eval()'d code on line 8
最佳答案
你可以用额外的要求来包装你的类(class)。将您的内容放到 other_file.php 并将其包含在模块文件中。
或者在模块文件中使用 eval 包装您的代码。 :^)
关于php - 绕过 eval() T_USE 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25155643/