简体   繁体   English

在 Prestashop 模块中使用命名空间

[英]Using namespaces in Prestashop module

How we can make the Prestashop module compatible with version 1.6 that uses namespaces, As I'm looking into the Prestashop documentation which says PrestaShop 1.6 does not fully support namespaces. They throw some issues when used in specific places.我们如何使 Prestashop 模块与使用命名空间的 1.6 版兼容,因为我正在查看 Prestashop 文档,其中说PrestaShop 1.6 does not fully support namespaces. They throw some issues when used in specific places. PrestaShop 1.6 does not fully support namespaces. They throw some issues when used in specific places. Is there any alternative way to this?有什么替代方法吗? Ref: https://devdocs.prestashop.com/1.7/modules/core-updates/1.6/参考: https://devdocs.prestashop.com/1.7/modules/core-updates/1.6/

You don't need to use namespace or "use" word for the main file.您不需要对主文件使用命名空间或“使用”字样。

I think you can use just full name in you code for example:我认为您可以在代码中只使用全名,例如:

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

or if you want to use namespace as you want.或者,如果您想根据需要使用命名空间。 you can make an empty class for the main file and make a class with your namespace for the parent.您可以为主文件创建一个空的 class 并为父文件使用您的命名空间创建一个 class。

so we have modules/yourmodule/yourmodule.php as the main file所以我们有 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.  
    
}

in modules/yourmodule/classes/yourmoduleparent.php在模块/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
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM