zoukankan      html  css  js  c++  java
  • 写一个PHP单例模式

     1 <?php
     2 /**
     3  * Created by PhpStorm. 5  * Date: 2019/1/29
     6  * Time: 17:44
     7  */
     8 
     9 namespace App\Models;
    10 
    11 use DfaFilter\SensitiveHelper;
    12 use App\Models\Sensitive;
    13 
    14 class StringHelper {
    15 
    16     static private $instance;
    17     static private $handle;
    18 
    19     //敏感词库不能为空,所以必须要有默认值
    20     private function getWordArray(){
    21         $sensitive = new Sensitive;
    22         $wordData = $sensitive ->getAllWordsCache();
    23         return $wordData;        
    24     }
    25 
    26     private function __construct(){
    27         self::$handle = SensitiveHelper::init()->setTree($this->getWordArray());
    28     }
    29 
    30     static public function getInstance(){
    31         if(!self::$instance instanceof self){
    32             self::$instance = new self();
    33         }
    34         return self::$instance;
    35     }
    36 
    37     //敏感词用*代替
    38     public function replace($content){
    39         if(empty($content)){
    40             return $content;
    41         }
    42         $content = self::$handle->replace($content,"***");
    43         return $content;
    44     }
    45 
    46     //判断是否包含敏感词 
    47     public function islegal($content){
    48         return self::$handle->islegal($content);
    49     }
    50 
    51     
    52 
    53 
    54 }
  • 相关阅读:
    HashMap、ConcurrentHashMap红黑树实现分析
    分布式系统ID
    分布式事务
    LRU算法实现
    Redis 深入
    分库分表利器——sharding-sphere
    Java常用的八种排序算法
    浅析Tomcat
    Kafka
    如何选择分布式事务形态
  • 原文地址:https://www.cnblogs.com/lianruihong/p/15580691.html
Copyright © 2011-2022 走看看