问题描述
图书馆
class getLang {
public function getText($en, $ar) {
$CI =& get_instance();
$lang = $CI->session->userdata('language');
if ($lang == 'en') {
$string = $en;
} else if ($lang == 'ar') {
if (!empty($ar)) {
$string = $ar;
} else if (empty ($ar)) {
$string = $en;
}
}
return $string;
}
}
控制器
class Home extends MY_Controller {
public function test() {
$this->load->library('getLang');
return $this->getLang->getText('text', '');
}
}
当我运行home / test时,
When I run home/test I get this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Home::$getLang
Filename: controllers/home.php
Line Number: 6
我已经在config / autoload中加载了自定义库。
看起来像库没有加载,我也试过加载它在控制器,仍然doens't工作!
我如何解决这个问题?
I already loaded the custom library in config/autoload.Looks like the library isn't loaded, I also tried loading it in the controller, still doens't work!How can I solve this?
推荐答案
CodeIgniter的文档清楚地说明类名和文件名必须大写。从:
CodeIgniter's documentation clearly states that class name and file name must be capitalize. From the documentation:
- 文件名必须大写。例如:Myclass.php
- 类声明必须大写。例如:class Myclass
- 类名和文件名必须匹配。
这可能是为什么它没有正确加载。
That's probably why it isn't correctly loaded.
在旁注,一个名为 getLang
听起来错误。这听起来像一个方法。我会建议一个名字,例如 Language
, Translator
,...
On a side note, a class named getLang
sounds wrong. It sounds like a method. I would suggest a name such as Language
, Translator
, ...
编辑:文档还要求使用名称的小写版本访问您的库。示例: $ this-> getlang-> getText(...)
The documentation also asks to access your library by using the lower case version of the name. Example: $this->getlang->getText(...)
这篇关于无法加载Codeigniter自定义库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!