开发者

How to load helper from model in CodeIgniter?

I w开发者_StackOverflowant to load some helper in a model. How to do this? Tried to use:

${get_parent_class($this)}->load->helper ('text');

But still getting an error

Fatal error: Call to a member function helper() on a non-object


GSto answered $this->load->helper('helpername') but if you are in a model's method, $this simply refers to that model's (class) instance and not to CI global. That won't work!

Instead you need to load the CI global and then load the helper:

// PHP 4
// $ci =& get_instance();
// PHP 5    
$ci = get_instance();
$ci->load->helper('text');


You are not need to load helper in a model.Just load helper in a controller and use function in a model as well as we normally use helper function in a controller


$this->load->helper('helpername')


I think CI doesnt check for helper duplication ... CI herlpers are procedural files , you might include ur helper twice if ur controller has the same helper loaded as ur model (that is loaded in that controller). Maybe do a library instead...

I can see i get negative votes w/o any comments ... by checking loader class from core CI u can see helpers method isnt checking if the helper has been loaded before (it isn't included in is_loaded() array like most classes that are loaded through load factory class) ... I dont recommend anyway to load helpers in both models and controllers ... for ex i made a helper for output encoding that i use in controllers (before i pass data to the view) . It would be very bad if i change the view state twice ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜