为了让所写函数能够动态调用,我设计了这样一个函数:
protected function _custom_list(&$model,$method,$param='') { //设置过滤 if (!empty($this->filters)){ $model->_setFilter($this->filters); if(!is_array($this->filters)) $p->parameter .= urlencode($this->filters) . "&";//分页跳转的时候保证查询条件 } $pagenation = $this->_get_pagenation(); //获取记录总数用于计算分页 if(!empty($param)) $count = count($model->$method($param)); else $count = count($model->$method()); $records=array(); if ($count > 0) { //生成分页代码 $temp_param = array($count, $pagenation['r']); //dwz_page类的构造函数参数 $p = $this->load->library('dwz_page', $temp_param); //页面分页序号显示 $this->template->set('page', $p->show()); //设置当前分页 $model->limit($p->listRows, $p->firstRow); //设置排序 if (!empty($pagenation['order'])) $model->order_by($pagenation['order'], $pagenation['dir']); if(!empty($param)) $records = $model->$method($param); else{ $records = $model->$method(); } } $this->template->set('numPerPage',$pagenation['r']); $this->template->set('records', $records); $this->template->set('postUrl', current_url()); $this->template->set('totalCount',$count); $this->template->set('order', $pagenation['order']); $this->template->set('sort', $pagenation['dir']); $this->template->set('currentPage', !empty($pagenation['p']) ? $pagenation['p'] : 1); }
这样解决了当调用的函数有个参数的情况。
在使用的过程中发现调用的方法经常需要传递多个参数,且参数的数量不一定,这很正常,但如何解决这个问题?
查看PHP手册后,两个方法让我的疑惑直接消散:
mixed call_user_func_array ( callable $callback , array $param_arr )
mixed call_user_func ( callable $callback [, mixed $parameter [, mixed $... ]] )
调用某个类中的某个方法示例:
function foobar($arg, $arg2) { echo __FUNCTION__, " got $arg and $arg2\n"; } class foo { function bar($arg, $arg2) { echo __METHOD__, " got $arg and $arg2\n"; } } // Call the foobar() function with 2 arguments call_user_func_array("foobar", array("one", "two")); // Call the $foo->bar() method with 2 arguments $foo = new foo; call_user_func_array(array($foo, "bar"), array("three", "four"));
更新后的函数为:
protected function _custom_list2(&$model,$method,$param='') { //设置过滤 if (!empty($this->filters)){ $model->_setFilter($this->filters); if(!is_array($this->filters)) $p->parameter .= urlencode($this->filters) . "&";//分页跳转的时候保证查询条件 } $pagenation = $this->_get_pagenation(); //获取记录总数用于计算分页 if(!empty($param)) $count= count((is_array ($param))?call_user_func_array(array($model,$method),$param):call_user_func(array($model,$method),$param)); //$count = count($model->$method($param)); else $count = count($model->$method()); $records=array(); if ($count > 0) { //生成分页代码 $temp_param = array($count, $pagenation['r']); //dwz_page类的构造函数参数 $p = $this->load->library('dwz_page', $temp_param); //页面分页序号显示 $this->template->set('page', $p->show()); //设置当前分页 $model->limit($p->listRows, $p->firstRow); //设置排序 if (!empty($pagenation['order'])) $model->order_by($pagenation['order'], $pagenation['dir']); if(!empty($param)) $records = (is_array ($param))?call_user_func_array(array($model,$method),$param):call_user_func(array($model,$method),$param); else{ $records = $model->$method(); } } $this->template->set('numPerPage',$pagenation['r']); $this->template->set('records', $records); $this->template->set('postUrl', current_url()); $this->template->set('totalCount',$count); $this->template->set('order', $pagenation['order']); $this->template->set('sort', $pagenation['dir']); $this->template->set('currentPage', !empty($pagenation['p']) ? $pagenation['p'] : 1); }