CodeIgniter: Deciding to Create a Library and Helper in CodeIgniter

After developing in CodeIgniter, it’s hard for me to make decisions when to create a user library and when to create a user assistant.

I understand that both allow you to have business logic in yourself and can be reused through the framework (calling from another controller, etc.).

But I strongly believe that the fact that the main CI developers separate the libraries from the helpers should be the reason for this, and I think this is the reason waiting for me to open and enlighten.

CI developers there, advise PLS.

I think it’s better to include an example.

I could have

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

Alternatively, I could also include all the functions in an assistant.

In the notification_helper.php file, I will include set_message(), get_message(), update_message() ..

In any case, you can still reuse it. Thus, it made me think about how to decide when exactly we are creating the library and the assistant, especially in CI.

In a normal (frameless) php application, the choice is clear, since there is no helper, you just need to create a library to reuse the codes. But here in CI, I would like to understand the main developers of the library sections and helpers

+44
design design-patterns codeigniter
Mar 02 '10 at 6:52
source share
6 answers

A question arises that I ask myself when, having decided this, that, I think, will also help you. The question is: am I providing a function to my structure or am I consolidating?

If you have a function that you add to your framework, then you will want to create a library for it. For example, form validation is a function that you add to a framework. Even if you can perform form validation without this library, you create a standard system for validation, which is a function.

However, there is also a form helper that helps you create HTML forms. The big difference from the form validation library is that the form helper does not create a new function, it is just a set of related functions that will help you write HTML forms correctly.

I hope this differentiation will help you, like me.

+25
Mar 02 '10 at 17:12
source share
β€” -

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

If you just have a group of functions, you just need to create a group of functions. If there is a lot of data in this function group, you need to create a class in which there is an instance for storing this data between method calls (class functions).

Do you have many public or private storage properties related to your notifications?

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

+57
Mar 02 '10 at 8:36
source share

First , you must be sure that you understand the difference between CI libaray and the helper class. The helper class is something that helps any finished thing, like array , string , uri , etc .; they are, and PHP already provides functions for them, but you still create an assistant to add more functionality to them. On the other hand, libaray may be something like what you are creating for the first time, any solution that may not necessarily be there.

Once you fully understand this difference, making a decision should not be so difficult.

Hope this helps.

thank

+7
Mar 02
source share

The assistant contains a group of functions that will help you complete a specific task.

Available Assistants in CI

Libraries typically contain non-CI specific functions. Like an image library. Something that is portable between applications.

Available Libraries in CI

Source Link

+3
May 6 '13 at 9:32
source share

If someone asks me how you will follow when the time comes to create helpers or libraries.

I think these differences are:

  • Class: In a nutshell, a class is a project for an object. And the object encapsulates a conceptually related state and responsibility of something in your application and usually offers a programming interface with which you can interact with them. This promotes code reuse and maintainability.
  • Functions: a function is a piece of code that takes another input as a parameter and performs some processing and returns a value. You have already seen many functions like fopen () and fread (), etc. They are built in functions, but PHP gives you the ability to create your own functions.

So go for Class , i.e. if any point matches

  • a global variable should be used in two or more functions or even in one, I hate using the Global keyword
  • default initialization according to each call or load
  • some tasks are private for entities that do not open publicly, think that functions never have public modifiers, why? Function
  • for dependencies to work, that is, tasks are separated, but she needs two or more tasks. Think of validate_email validation only to send an email script for, cc, bcc, etc. all this requires validate_email.
  • And finally, not all related tasks, that is, functions should be placed in one object or file, are easier to reference and remember.

For assistants: any point that does not match libraries

+2
Aug 24 '16 at 12:15
source share

Personally, I use libraries for big things, say, the FTP library I built is much faster than the supplied CodeIgniters library. This is a class with many methods that communicate with each other.

I use helpers for small tasks that are not related to many other functions. An example is small functions such as decorating strings. Or copy the directory recursively to another location.

0
Mar 02 '10 at 8:20
source share



All Articles