difference between libraries and helpers in php fr

2019-01-24 06:35发布

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?

5条回答
forever°为你锁心
2楼-- · 2019-01-24 07:14

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.

查看更多
可以哭但决不认输i
3楼-- · 2019-01-24 07:16

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!

查看更多
虎瘦雄心在
4楼-- · 2019-01-24 07:25

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

If they are functions, why do you want to stick them in a class? PHP allows for free floating functions.

查看更多
Juvenile、少年°
5楼-- · 2019-01-24 07:32

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.)

查看更多
ら.Afraid
6楼-- · 2019-01-24 07:34

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.

查看更多
登录 后发表回答