user179432
user179432

Reputation: 463

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

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

Upvotes: 45

Views: 20032

Answers (6)

Sarfraz
Sarfraz

Reputation: 382696

First of all, you should be sure that you understand the difference between CI library and helper class. Helper class is anything that helps any pre-made thing such as array, string, uri, etc; they are there and PHP already provides functions for them but you still create a helper to add more functionality to them.

On the other hand, library can be anything like something you are creating for the first time, any solution which might not be necessarily already out there.

Once you understand this difference fully, taking decision must not be that difficult.

Upvotes: 7

mrsrinivas
mrsrinivas

Reputation: 35404

Helper contains a group of functions to help you do a particular task.

Available helpers in CI

Libraries usually contain non-CI specific functionality. Like an image library. Something which is portable between applications.

Available libraries in CI

Source link

Upvotes: 4

AmmyTech
AmmyTech

Reputation: 738

If someone ask me what the way you follow when time comes to create Helpers or Libraries.

I think these differences:

  • Class : In a nutshell, a Class is a blueprint for an object. And an object encapsulates conceptually related State and Responsibility of something in your Application and usually offers an programming interface with which to interact with these. This fosters code reuse and improves maintainability.
  • Functions : A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value. You already have seen many functions like fopen() and fread() etc. They are built-in functions but PHP gives you option to create your own functions as well.

So go for Class i.e. libraries if any one point matches

  1. global variable need to use in two or more functions or even one, I hate using Global keyword
  2. default initialization as per each time call or load
  3. some tasks are private to entity not publicly open, think of functions never have public modifiers why?
  4. function to function dependencies i.e. tasks are separated but two or more tasks needs it. Think of validate_email check only for email sending script for to,cc,bcc,etc. all of these needs validate_email.
  5. And Lastly not least all related tasks i.e. functions should be placed in single object or file, it's easier for reference and remembrance.

For Helpers : any point which not matches with libraries

Upvotes: 2

Seaux
Seaux

Reputation: 3517

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.

Upvotes: 29

Phil Sturgeon
Phil Sturgeon

Reputation: 30766

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.

Upvotes: 59

Christoffer
Christoffer

Reputation: 26825

Personally I use libraries for big things, say an FTP-library I built that is a lot faster than CodeIgniters shipped library. This is a class with a lot of methods that share data with each other.

I use helpers for smaller tasks that are not related to a lot of other functionality. Small functions like decorating strings might be an example. Or copying a directory recursively to another location.

Upvotes: 1

Related Questions