我在'custom_functions.php'中有一些自定义函数,我从控制器调用它。
我的'custom_functions.php'代码如下:
class Custom_functions extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('model');
$this->load->library('session');
}
public function data($first, $next) {
// other codes start from here
}
}
我将该文件保存在应用程序/库中并加载到控制器中:
class Home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('model');
$this->load->library('pagination');
$this->load->library('custom_functions');
}
// other codes start from here
}
然后我的自定义功能起作用,但分页不起作用。
我收到错误消息:
A PHP Error was encountered Severity: Notice Message: Undefined property: CI_Loader::$pagination Filename: views/index.php Line Number: 27
视图文件第27行是:
echo $this->pagination->create_links();
如果我删除
$this->load->library('custom_functions');
然后分页线工作。为什么会这样?加载我的自定义函数或者将自定义函数保存在错误的文件夹中我做错了吗?我应该在哪个文件夹中保留'custom_functions.php'文件?
答案 0 :(得分:4)
您需要实例化您的库
class sample_lib
{
private $CI=null
function __construct()
{
$this->CI=& get_instance();
}
}
class Custom_functions {
private $CI = null;
public function __construct() {
$this->CI=& get_instance();
parent::__construct();
$this->CI->load->model('model');
$this->CI->load->library('session');
}
public function data( $first, $next ) {
// other codes start from here
}
}
然后打电话给你的控制器:
echo $this->Custom_functions->data( $first, $next );
答案 1 :(得分:0)
当您创建库时,您不应该使用CI_Controller类来扩展它。因此,您的库将包含以下代码
class Custom_functions {
public function __construct() {
parent::__construct();
//and you cannot you '$this' while working in the library functions so
$CI =& get_instance();
//now use $CI instead of $this
$CI->load->model('model');
$CI->load->library('session');
}
public function data($first, $next) {
// other codes start from here
}
}