2017-05-09 26 views
0

通常,當你在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

回答

0

你可以做什麼調用父構造函數,並使其他參數可選。唯一的問題是,你仍然不會使用這個新的參數,因爲加載函數不允許不更改加載器。

實施例構造器在裝載機

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(); 
    } 
} 
相關問題