php框架中库和助手的区别

2022-01-16 00:00:00 frameworks php

如果我有我经常使用的字符串函数,我应该将它们放在帮助类还是库类中?

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

函数如:截断超过 30 个字符的字符串、返回随机字符串、全部小写等等.这些是我可能不需要为其创建对象的功能.最好将它们用作静态方法.

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?

推荐答案

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

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

数组
字符串
网址
等等

库可以是任何解决方案;它可能是您第一次创建的,没有其他人创建过.

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.

相关文章