• 1

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

There is a question I ask myself when deciding this that I think will help you as well. The question is: Am I providing a feature to my framework or am I consolidating?

If you have a feature that you are adding to your framework, then you'll want to create a library for that. Form validation, for example, is a feature that you are adding to a framework. Even though you can do form validation without this library, you're creating a standard system for validation which is a feature.

However, there is also a form helper which helps you create the HTML of forms. The big difference from the form validation library is that the form helper isn't creating a new feature, its just a set of related functions that help you write the HTML of forms properly.

Hopefully this differentiation will help you as it has me.

  • 1
Reply Report