加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP中的助手类

发布时间:2020-12-13 16:39:08 所属栏目:PHP教程 来源:网络整理
导读:我想创建一个包含cleanArray,split_char,split_word等方法的帮助类. 它自助的助手类将与许多类一起使用.示例: Class A will user Helper,Class B,Class C,D,E also user Helper Class 在PHP中编写和使用帮助类的最好方式是什么? 我知道的是OOP的基本知识,
我想创建一个包含cleanArray,split_char,split_word等方法的帮助类.

它自助的助手类将与许多类一起使用.示例:

Class A will user Helper,Class B,Class C,D,E also user Helper Class

在PHP中编写和使用帮助类的最好方式是什么?

我知道的是OOP的基本知识,在使用Helper类的每个类中都必须创建一个帮助对象.

$helper = new Helper();

这个对或可能是一个可以给我最好的方法来做到这一点.

我也将创建可以使用A,B,C等的XXX类

更新: – >修复我的split_word方法的错误:D

基于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 )));
    }
}
?>

我在其他类中使用

<?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   ? ?  .,}{ + _"));
?>
根据经验,帮助者应该包含常见的功能,但在应用程序的整体架构下没有特殊的指定.

>使用Helper后缀名
>尽可能使用静态方法

简而言之:

// Helper sample
//
class ConversionHelper {

   static function helpThis() {
      // code
   }

   static function helpThat() {
      // code
   }
}

// Usage sample
//
class User {

   function createThings() {
      $received = ConversionHelper::helpThis();
   }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读