Codeigniter 自定义库加载错误
Codeigniter Custom Library loading error
我的自定义库无法通过自动加载或手动代码加载。我检查了 this one and Documentation。
library/Faq.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Faq {
public function __construct(){
$this->load->model('Faq_model');
}
public function recentFaqs(){
return $this->Faq_model->getFaqs();
}
}
Loaded the library $this->load->library('faq');
Called its function $this->faq->recentFaqs();
我收到以下错误
遇到 PHP 错误
Severity: Notice
Message: Undefined property: Faq::$faq
Filename: controllers/Faq.php
Line Number: 17
问题可能是因为您的构造函数正在尝试使用 $this
加载模型。但是 $this
需要引用 CodeIgniter。因此,您需要在加载模型之前获取 CodeIgniter 引用。
class Faq {
public $CI;
public function __construct(){
$this->CI = & get_instance();
$this->CI->load->model('Faq_model');
}
public function recentFaqs(){
return $this->Faq_model->getFaqs();
}
}
对 CI $this->CI
的引用应该用于此 class 的任何将调用 codeigniter classes 的函数中。通过将其保存为 class 的 属性,可以轻松重复使用。
我的自定义库无法通过自动加载或手动代码加载。我检查了 this one and Documentation。
library/Faq.php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Faq {
public function __construct(){
$this->load->model('Faq_model');
}
public function recentFaqs(){
return $this->Faq_model->getFaqs();
}
}
Loaded the library $this->load->library('faq');
Called its function $this->faq->recentFaqs();
我收到以下错误 遇到 PHP 错误
Severity: Notice
Message: Undefined property: Faq::$faq
Filename: controllers/Faq.php
Line Number: 17
问题可能是因为您的构造函数正在尝试使用 $this
加载模型。但是 $this
需要引用 CodeIgniter。因此,您需要在加载模型之前获取 CodeIgniter 引用。
class Faq {
public $CI;
public function __construct(){
$this->CI = & get_instance();
$this->CI->load->model('Faq_model');
}
public function recentFaqs(){
return $this->Faq_model->getFaqs();
}
}
对 CI $this->CI
的引用应该用于此 class 的任何将调用 codeigniter classes 的函数中。通过将其保存为 class 的 属性,可以轻松重复使用。