PHP 中的辅助类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7064867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Helper Class in PHP
提问by Ahmad
I want create a helper class that containing method like cleanArray, split_char, split_word, etc.
我想创建一个辅助类,其中包含像 cleanArray、split_char、split_word 等方法。
The helper class it self will be used with many class. example :
它自己的助手类将与许多类一起使用。例子 :
Class A will user Helper, Class B, Class C, D, E also user Helper Class
what the best way to write and use helper class in PHP ?
在 PHP 中编写和使用辅助类的最佳方法是什么?
what i know is basic knowledge of OOP that in every Class that use Helper class must create a helper object.
我所知道的是 OOP 的基本知识,即在每个使用 Helper 类的类中都必须创建一个 helper 对象。
$helper = new Helper();
It that right or may be some one can give me best way to do that.
它是正确的,或者可能是某个人可以给我最好的方法来做到这一点。
I also will create XXX Class that may use Class A, B, C, etc.
我还将创建可能使用 A、B、C 类等的 XXX 类。
UPDATE :->FIXED my fault in split_word method :D
更新:->修复了我在 split_word 方法中的错误:D
Based on Saul, Aram Kocharyan and alex answer, i modified my code, but its dont work, i dont know why.
根据 Saul、Aram Kocharyan 和 alex 的回答,我修改了我的代码,但它不起作用,我不知道为什么。
<?php
class Helper {
static function split_word($text) {
$array = mb_split("\s", preg_replace( "/[^\p{L}|\p{Zs}]/u", " ", $text ));
return $this->clean_array($array);
}
static function split_char($text) {
return preg_split('/(?<!^)(?!$)/u', mb_strtolower(preg_replace( "/[^\p{L}]/u", "", $text )));
}
}
?>
and i use in other Class
我在其他班级使用
<?php
include "Helper.php";
class LanguageDetection {
public function detectLanguage($text) {
$arrayOfChar = Helper::split_char($text);
$words = Helper::split_word($text);
return $arrayOfChar;
}
}
$i = new Detection();
print_r($i->detectLanguage("ab cd UEEef する ? ???? ?? 12 34 ? ? .,}{ + _"));
?>
采纳答案by Saul
As a rule of thumb, helpers should contain functionality that is common but has no special designation under the overall architecture of the application.
根据经验,助手应包含通用但在应用程序的整体架构下没有特殊指定的功能。
- Suffix the classname with
Helper
- Use static methods whenever possible
- 在类名后缀
Helper
- 尽可能使用静态方法
In short:
简而言之:
// Helper sample
//
class ConversionHelper {
static function helpThis() {
// code
}
static function helpThat() {
// code
}
}
// Usage sample
//
class User {
function createThings() {
$received = ConversionHelper::helpThis();
}
}
回答by Gordon
Helper classes are usually a sign of lack of knowledge about the Model's problem domain and considered an AntiPattern (or at least a Code Smell) by many. Move methods where they belong, e.g. on the objects on which properties they operate on, instead of collecting remotely related functions in static classes. Use Inheritance for classes that share the same behavior. Use Composition when objects are behaviorally different but need to share some functionality. Or use Traits.
Helper 类通常表示对模型的问题域缺乏了解,并被许多人视为反模式(或至少是代码气味)。将方法移到它们所属的地方,例如在它们操作属性的对象上,而不是在静态类中收集远程相关的函数。对共享相同行为的类使用继承。当对象在行为上不同但需要共享某些功能时,请使用组合。或者使用特征。
The static Utils class you will often find in PHP is a code smell. People will throw more or less random functions into a class for organizing them. This is fine when you want to do procedural coding with PHP<5.2. As of 5.3 you would group those into a namespaceinstead. When you want to do OOP, you want to avoid static methods. You want your objects to have High Cohesion and Low Coupling. Static methods achieve the exact opposite. This will also make your code less testable.
您在 PHP 中经常会发现的静态 Utils 类是一种代码异味。人们会或多或少地将随机函数扔到一个类中来组织它们。当您想使用 PHP<5.2 进行程序编码时,这很好。从 5.3 开始,您可以将它们分组到一个命名空间中。当你想做 OOP 时,你想避免静态方法。您希望您的对象具有高内聚和低耦合。静态方法实现了完全相反的效果。这也会降低您的代码的可测试性。
- Are Helper Classes Evil?
- Killing the Helper class, part two
- Functional Decomposition AntiPattern
- Is the word "Helper" in a class name a code smell?
Moreover, every Class that use Helper class must create a helper objectis a code smell. Your collaborators should not create other collaborators. Move creation of complex object graphs into Factories or Builders instead.
而且,每个使用 Helper 类的 Class 都必须创建一个 helper 对象,这是一种代码异味。您的合作者不应创建其他合作者。将复杂对象图的创建移到工厂或构建器中。
回答by tere?ko
Instead of creating static class , you should just write simple functions , and include that file at the index/bootstrap file (you can even use namespaces with it).
您应该只编写简单的函数,而不是创建静态类,并将该文件包含在索引/引导文件中(您甚至可以使用命名空间)。
Instead of:
代替:
class Helper {
static function split_word($text) { ...
static function split_char($text) { ...
}
It should be:
它应该是:
namespace Helper;
function split_word($text) { ...
function split_char($text) { ...
There is no point wrapping it all up in a class. Just because you put it in a class doesn't make it object oriented .. actually it does the exact oposite.
把它全部放在一个类中是没有意义的。仅仅因为你把它放在一个类中并不能使它面向对象......实际上它完全相反。
回答by alex
You could create a class with static methods...
您可以使用静态方法创建一个类...
class Str {
public static function split_char($str, $chr) {
...
}
}
You could also namespace a bunch of functions with a namespace
, but I think the former is preferred.
你也可以用 a 命名一堆函数namespace
,但我认为前者是首选。
回答by Aram Kocharyan
Use public static methods in the class as such:
在类中使用公共静态方法,如下所示:
/* Common utility functions mainly for formatting, parsing etc. */
class CrayonUtil {
/* Creates an array of integers based on a given range string of format "int - int"
Eg. range_str('2 - 5'); */
public static function range_str($str) {
preg_match('#(\d+)\s*-\s*(\d+)#', $str, $matches);
if (count($matches) == 3) {
return range($matches[1], $matches[2]);
}
return FALSE;
}
// More here ...
}
Then invoke them like this:
然后像这样调用它们:
CrayonUtil::range_str('5-6');
If in another file, use the following at the top:
如果在另一个文件中,请在顶部使用以下内容:
require_once 'the_util_file.php';