CodeIgniter, Is it possible to use an helper withi

2019-04-15 04:36发布

问题:

I am writting my custom helper. I tried to use the language helper:

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

I get an error :

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

回答1:

If you want to call methods from the CodeIgniter super object within a helper (or a custom library) you'll need to use the get_instance() function. This will reference the CodeIgniter super object to the variable $ci - so you can call the CodeIgniter methods by using $ci rather than $this:

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


回答2:

Or you can also use the language helper:

$site_title = lang('site_title');

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

That is assuming the helper before this point. Otherwise just do as above.