CodeIgniter: Decision making for creating of library & helper in CodeIgniter

codeigniterdesign-patterns

After developing in CodeIgniter for awhile, I find it difficult to make decisions when to create a custom library and when to create a custom helper.

I do understand that both allow having business logic in it and are reusable across the framework (calling from different controller etc.)

But I strongly believe that the fact that CI core developers are separating libraries from helpers, there has to be a reason behind it and I guess, this is the reason waiting for me to discover and get enlightened.

CI developers out there, pls advise.

i think it's better to include an example.

I could have a

class notification_lib {
   function set_message() { /*...*/}

   function get_message() {/*...*/}

   function update_message() {/*...*/}
}

Alternatively, i could also include all the functions into a helper.

In a notification_helper.php file, i will include set_message(), get_message(), update_message()..

Where either way, it still can be reused. So this got me thinking about the decision making point about when exactly do we create a library and a helper particularly in CI.

In a normal (framework-less) php app, the choice is clear as there is no helper, you will just need to create a library in order to reuse codes. But here, in CI, I would like to understand the core developers seperation of libraries and helpers

Best Answer

Well the choice comes down to set of functions or class. The choice is almost the same as a instance class verses a static class.

If you have just a simply group of functions then you only need to make a group of functions. If these group of functions share a lot of data, then you need to make a class that has an instance to store this data in between the method (class function) calls.

Do you have many public or private properties to store relating to your notification messages?

If you use a class, you could set multiple messages through the system then get_messages() could return a private array of messages. That would make it perfect for being a library.

Related Topic