我试图组织我的控制器&模型,并将通用代码移动到父类。我设法组织了我的模型,但现在我坚持组织控制器。如何在Child控制器类中加载模型并在父类中使用?
我父控制器是:
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
protected $model;
public function __construct(){
parent::__construct();
}
public function getDataByCity(){
echo(json_encode($this->model->getDataByCity()));
}
}?>
我的孩子控制器是这样的:
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Hospital extends MY_Controller {
public function __construct(){
parent::__construct();
$this->load->model('hospital_model');
//how do I load this into the Parent's $model variable?
}
}
}?>
在我的孩子控制器类,我怎么特定模型加载到父的$模型变量?
试试这个:http://stackoverflow.com/questions/12040864/modify-parent-classs-variable-in-extended-class :) –
我不认为这解决了我的问题,因为模型加载Codeignitor以及用该名称创建的变量。 –
尝试指定第二个参数以将模型分配给不同的对象名称$ this-> load-> model('hospital_model','model'); ' –