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 クラスで拡張することは想定されていません。したがって、ライブラリには次のコードが含まれます

クラス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
}

}

于 2013-08-28T12:41:27.480 に答える