php框架中库和助手之间的区别

时间:2022-06-17 13:28:52

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.

函数如:截断字符串如果超过30个字符,返回一个随机字符串,使所有小写的情况等等。这些是我可能不需要为其创建对象的函数。最好将它们用作静态方法。

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

我应该把它们放在库类或帮助类中吗?

when do i know when to put where?

我什么时候知道何时放在哪里?

5 个解决方案

#1


13  

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

助手是帮助已经存在的东西的类,例如可以有一个助手:

array
string
url
etc

数组字符串url等

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.

因为你正在处理一个字符串(已存在的东西),你应该把它放在一个帮助器类中,或者修改框架的字符串辅助类(如果有的话)。但是,这是一个约定或标准,但如果您正在创建一些非常酷的字符串处理功能,那么您也可以为它创建一个库。

#2


11  

Besides the manual that explains these all quite well…

除了手册解释这些都很好...

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

libraries:对象状态很重要的实用程序类(支付网关,身份验证等)

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

helper:执行重复任务的相关函数(不是类)的集合(字符串,数组等)

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

插件:放入第三方类的简单方法。通常,使用单个包装函数调用整个过程。 (在即将推出的CodeIgniter 2.0版中已弃用。)

#3


3  

I assume you are using CodeIgniter.

我假设您正在使用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.

既然你已经写过你不需要实例化一个对象并且会在它的静态方法中使用它,那么将它变成helper将比将它变成库更有意义。

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.

在CI中,也管理帮助程序,一旦加载,将省略第二次加载它的尝试。您可以在帮助程序中打开CI的构建,以了解它的作用,然后将其与库进行比较。通过了解目的,您可以决定自己,帮助者或图书馆。

#4


2  

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!

Helper是用户定义或预定义函数的集合,不需要实例化库以及类需要实例化以使用它们。库也可能包含用户定义和预定义的函数/方法。库(类)中定义的函数称为方法!

#5


1  

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.

如果它们是函数,为什么要将它们粘在一个类中呢? PHP允许*浮动功能。

#1


13  

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

助手是帮助已经存在的东西的类,例如可以有一个助手:

array
string
url
etc

数组字符串url等

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.

因为你正在处理一个字符串(已存在的东西),你应该把它放在一个帮助器类中,或者修改框架的字符串辅助类(如果有的话)。但是,这是一个约定或标准,但如果您正在创建一些非常酷的字符串处理功能,那么您也可以为它创建一个库。

#2


11  

Besides the manual that explains these all quite well…

除了手册解释这些都很好...

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

libraries:对象状态很重要的实用程序类(支付网关,身份验证等)

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

helper:执行重复任务的相关函数(不是类)的集合(字符串,数组等)

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

插件:放入第三方类的简单方法。通常,使用单个包装函数调用整个过程。 (在即将推出的CodeIgniter 2.0版中已弃用。)

#3


3  

I assume you are using CodeIgniter.

我假设您正在使用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.

既然你已经写过你不需要实例化一个对象并且会在它的静态方法中使用它,那么将它变成helper将比将它变成库更有意义。

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.

在CI中,也管理帮助程序,一旦加载,将省略第二次加载它的尝试。您可以在帮助程序中打开CI的构建,以了解它的作用,然后将其与库进行比较。通过了解目的,您可以决定自己,帮助者或图书馆。

#4


2  

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!

Helper是用户定义或预定义函数的集合,不需要实例化库以及类需要实例化以使用它们。库也可能包含用户定义和预定义的函数/方法。库(类)中定义的函数称为方法!

#5


1  

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.

如果它们是函数,为什么要将它们粘在一个类中呢? PHP允许*浮动功能。