【问题标题】:Using namespaces in Prestashop module在 Prestashop 模块中使用命名空间
【发布时间】:2020-12-29 10:51:38
【问题描述】:

我们如何使 Prestashop 模块与使用命名空间的 1.6 版兼容,因为我正在查看 Prestashop 文档,上面写着 PrestaShop 1.6 does not fully support namespaces. They throw some issues when used in specific places. 有没有其他方法可以解决这个问题? 参考:https://devdocs.prestashop.com/1.7/modules/core-updates/1.6/

【问题讨论】:

    标签: php namespaces prestashop prestashop-1.6 prestashop-1.7


    【解决方案1】:

    您不需要对主文件使用命名空间或“使用”字样。

    我认为您可以在代码中只使用全名,例如:

    $data = \PrestaShop\Some\ClassName::getData();
    

    或者如果您想根据需要使用命名空间。 您可以为主文件创建一个空类,并为父文件创建一个使用您的命名空间的类。

    所以我们有 modules/yourmodule/yourmodule.php 作为主文件

    <?php
    
    if (!defined('_PS_VERSION_')) {
        exit;
    }
    
    require_once(dirname(__FILE__) .'/classes/yourmoduleparent.php');
    
    class YourModule extends \YourModule\Bootstrap {
        
        // The module codes have been transferred
        // to the "/classes/yourmoduleparent.php" file.  
        
    }
    

    在模块/yourmodule/classes/yourmoduleparent.php 中

    <?php
    
    namespace YourModule;
    
    if (!defined('_PS_VERSION_')) {
        exit;
    }
    
    use Module;
    use Configuration;
    use Context;
    use Tools;
    use Controller;
    use PrestaShopException;
    use Hook;
    
    class Bootstrap extends Module {
        
        // Your module codes
        
    }
    

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 2013-06-22
      • 1970-01-01
      • 2013-11-04
      • 2021-01-22
      • 2014-06-16
      • 2020-05-07
      • 2020-10-05
      • 2021-05-31
      相关资源
      最近更新 更多