如何在prestashop 1.6中覆盖构造函数和公共变量

时间:2018-06-06 11:30:23

标签: prestashop

我有一个自定义模块,它覆盖了Product类。这是我的代码:

class Product extends ProductCore {
    public $variable;

    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null) {
        parent::__construct($id_product, $full, $id_lang, $id_shop);

        self::$definition['fields']['variable'] = array('type' => self::TYPE_BOOL, 'validate' => 'isBool');
    }

}

安装模块时,没有错误。但是在override文件夹中,product.php文件我看不到

public $variable;

我需要自己添加它。问题在哪里?

感谢您的帮助。

-edit 这是来自以下答案的代码输出。像yuo看,没有公共$变量。为什么?

/*
* module: mymodule
* date: 2018-06-06 15:08:01
* version: 1.0.0
*/
public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null) {
    parent::__construct($id_product, $full, $id_lang, $id_shop);
    self::$definition['fields']['variable'] = array('type' => self::TYPE_BOOL, 'validate' => 'isBool');
}

2 个答案:

答案 0 :(得分:1)

解决。要解决此问题,您需要将构造函数放在首位。最后,添加变量声明。如下所示。

类产品扩展了ProductCore {

public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
    {        
        parent::__construct($id_product, $full, $id_lang, $id_shop);
        self::$definition['fields']['variable'] = array('type' => self::TYPE_BOOL, 'validate' => 'isBool');        
    }

public $variable;

}

答案 1 :(得分:0)

使用Prestashop的类声明编码标准(在自己的行中打开和关闭大括号),它将正确解析覆盖。

class Product extends ProductCore
{
    public $variable;

    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null) 
    {
        parent::__construct($id_product, $full, $id_lang, $id_shop);

        self::$definition['fields']['variable'] = array('type' => self::TYPE_BOOL, 'validate' => 'isBool');
    }
}