Welcome Guest, Not a member yet? Register   Sign In
helper or library?
#1

[eluser]introvert[/eluser]
Hello,

I have a controller function that should be called at multiple places (list_items) but I'm unsure if I should make a helper or library for it.

I should load a library within this function (table library) so I have a few concerns if this is possible with helpers since they're not separated objects to that I could call $this on them..

What should I do?

Heres some of the code:

Code:
//add rows to the table
$this->table->set_heading(array_keys($row));    
$this->load->view('list', $data);

Is there any way to make it work like helper? Would it make sense?

Thanks for help!
#2

[eluser]BrianDHall[/eluser]
My relatively 'loose' opinion is it'd be better to make it a library so you can explicitly use get_instance() to control access to the super-object instead of relying on $this to behave as you would expect in all controllers, models, views, or anywhere else you might want to use it.

Helpers in general should be things that don't really care about things like controllers, models, views, $this, and that shouldn't need to keep track of anything like internal variables, etc. Violating this can easily create bugs that are hard to find, as helpers should generally take certain parameters and their only effect on the system as a whole should be returning a value.

Helpers are basically meant to mimick functional programming and built-in PHP functions like md5(), url_encode(), chomp(), trim(), etc etc.

Libraries are like CI's image manipulation or file upload classes - they are objects that often need to keep track of certain information like config, variable file paths, environment variables, etc.

If your helper requires other helpers, other objects, other libraries, and so on, I think it'll save you a lot of grief to just make them a library.
#3

[eluser]Colin Williams[/eluser]
You can use get_instance() in a helper, too. I put a lot of these view-related functions in a views_helper or theme_helper.

Code:
function list_items($row, $data, $return = TRUE)
{
  $ci =& get_instance();
  $ci->table->set_heading(array_keys($row));
  return $ci->load->view('list', $data, $return);
}




Theme © iAndrew 2016 - Forum software by © MyBB