CodeIgniter,是否可以在自定义帮助程序中使用帮助程序


CodeIgniter, Is it possible to use an helper within a custom helper?

我正在编写我的自定义助手。我尝试使用语言助手:

$this->lang->line('site_title')

我得到一个错误:

Fatal error: Using $this when not in object context in
C:'Users'guest'Wamp'www'codeIgniter'application'helpers'blog_helper.php on line 15

如果要从助手(或自定义库)中的CodeIgniter超级对象调用方法,则需要使用get_instance()函数。这将把CodeIgniter超级对象引用到变量$ci,因此您可以使用$ci而不是$this:来调用CodeIgnitor方法

$ci =& get_instance();
$site_title = $ci->lang->line('site_title');

或者您也可以使用语言助手:

$site_title=lang('site_title');

http://codeigniter.com/user_guide/helpers/language_helper.html

这是假设在此点之前的辅助对象。否则,请按上述操作。