1

我从控制器调用的“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”文件保存在哪个文件夹中?

4

2 回答 2

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 );
于 2013-08-28T01:08:44.080 回答
0

当你创建一个库时,你不应该使用 CI_Controller 类来扩展它。因此,您的库将包含以下代码

类自定义函数{

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
}

}

于 2013-08-28T12:41:27.480 回答