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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧