写一个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 }

 

posted on 2021-11-20 12:00  lianruihong  阅读(15)  评论(0编辑  收藏  举报

导航