Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between libraries and helpers in php frameworks

Tags:

php

frameworks

if i've got string functions i use a lot, should i put them in a helper class or a library class?

functions like: truncate string if longer than 30 characters, return a random string, make all lower cases and so on. these are functions that i probably don't need to create an object for. it's better to use them as static methods.

should i put them in a library class or a helper class?

when do i know when to put where?

like image 629
ajsie Avatar asked Feb 01 '10 05:02

ajsie


4 Answers

Helpers are the classes that help something already there for example there can be a helper for:

array
string
url
etc

A library is something that can be any solution; it could be created for the first time by you and no one else has created.

Because you are dealing with a string (something already there), you should put it in a helper class, or modify the string helper class of the framework (if there is one). However, this is a convention or standard but you can create a library for it too if you are creating something really cool for string handling with quite some functions.

like image 139
Sarfraz Avatar answered Oct 23 '22 16:10

Sarfraz


Besides the manual that explains these all quite well…

libraries: Utility classes where object state is important (payment gateways, authentication, etc.)

helpers: Collections of related functions (not classes) that do repetitive tasks (strings, arrays, etc.)

plugins: A simple way to drop in third party classes. Typically, the whole process is called with a single wrapper function. (deprecated in the upcoming version 2.0 of CodeIgniter.)

like image 44
nilesh Avatar answered Oct 23 '22 17:10

nilesh


I assume you are using CodeIgniter.

Since you already write that you don't need to instantiate an object and will use it in it's static methods, then making it into helper will be make sense than making it into library.

In CI, helpers is also managed, once loaded, second attempt to load it will be omitted. You can open the CI's build in helper to learn what it does, then compare it with libraries. By knowing the purpose, you can then decide yourself, helpers or libraries.

like image 26
Donny Kurnia Avatar answered Oct 23 '22 17:10

Donny Kurnia


Helper is collection of user-defined or pre-defined functions, no need to instantiate as well as libraries are classes needs to instantiate to use them. Library might contain user-defined and pre-defined functions/methods too. Function defined in library (class) is known as method!

like image 31
Adnan Avatar answered Oct 23 '22 18:10

Adnan