我试图通过变量名而不是硬编码的方法名来调用加载模型的方法。这将在我的控制器中为我提供大量抽象,而无需使用一堆 if-then 语句。
这是模型
class Reports_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function backlog()
{
//Do stuff
}
我想要的是能够通过变量名调用积压函数。这是控制器:
class Reports extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function get_reports($report_name)
{
$this->load->model('reports_model');
$report_name = 'backlog';
$data['data'] = $this->reports_model->$report_name();
}
据我所知(而且我可能遗漏了一些愚蠢的东西),我的代码与http://php.net/manual/en/functions.variable-functions.php上的示例 #2 完全相同,但我得到了函数调用行中的此错误:
未定义属性:Reports::$reports_model