简体   繁体   中英

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 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/

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.

so we have modules/yourmodule/yourmodule.php as the main file

<?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

<?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
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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