dqxboe2628 2013-08-28 00:45
浏览 143
已采纳

我应该在哪个文件夹中保留自定义函数文件?

I have some custom functions in 'custom_functions.php' which I call from my controllers.

my 'custom_functions.php' code is like :

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
}
}

I kept that file in application/libraries and loaded in controller:

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
}

then my custom function worked but the pagination wasn't working.

I get an error message:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Loader::$pagination

Filename: views/index.php

Line Number: 27

While the view file 27th line is:

echo $this->pagination->create_links();

if I remove

$this->load->library('custom_functions');

then the pagination line works. why this happens ? did I do wrong to load my custom function or I kept my custom function in wrong folder ? in which folder should I keep my 'custom_functions.php' file ?

  • 写回答

2条回答 默认 最新

  • duanchui1955 2013-08-28 01:08
    关注

    You need to instantiate your library

    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
        }
    }
    

    then to call in your controller:

    echo $this->Custom_functions->data( $first, $next );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败