如何在opencart中的模型类构造方法中传递参数?

问题描述:

通常,当你在Opencart的加载模式,你会做到以下几点:如何在opencart中的模型类构造方法中传递参数?

$this->load->model('example'); 
// calling the method inside the model 
$this->model_example->method(); 

然而,现在我有,有一个构造函数,这样的模式:

public function __construct($filepath, $timezone, $priority, $registry){ 
    // code... 
} 

正如你所看到的,我需要使用构造函数中所需的所有参数加载模型,那么在这种情况下如何加载我的模型?

+0

public function __construct($registry, $filepath='', $timezone='', $priority='') { parent::__construct($registry); // Your code here } 

模型函数是这个笨? – Demonyowh

+0

@Demonyowh不,它是opencart,但它们的语法非常相似 – SSD

你可以做什么调用父构造函数,并使其他参数可选。唯一的问题是,你仍然不会使用这个新的参数,因为加载函数不允许不更改加载器。

实施例构造器在装载机

public function model($model) { 
    $file = DIR_APPLICATION . 'model/' . $model . '.php'; 
    $class = 'Model' . preg_replace('/[^a-zA-Z0-9]/', '', $model); 

    if (file_exists($file)) { 
     include_once($file); 

     $this->registry->set('model_' . str_replace('/', '_', $model), new $class($this->registry)); 
    } else { 
     trigger_error('Error: Could not load model ' . $file . '!'); 
     exit(); 
    } 
}