功能:一个censor审核提交字符串的类(附加在DEDECMS里面)
两份不同的作品,同样的一种功能,大家评比一下~认为他们的水平如何?
如果你是主考官,你会如何选择?为什么?
涉及MySQL数据表:gk_info_censormysql> desc gk_info_censor;
+-------------+----------------------+------+-----+---------+----------------+
| Field       | Type                 | Null | Key | Default | Extra          |
+-------------+----------------------+------+-----+---------+----------------+
| id          | smallint(6) unsigned | NO   | PRI | NULL    | auto_increment |
| admin       | varchar(15)          | NO   |     |         |                |
| type        | smallint(6)          | NO   |     | 1       |                |
| find        | varchar(255)         | NO   | UNI |         |                |
| replacement | varchar(255)         | NO   |     |         |                |
| extra       | varchar(255)         | NO   |     |         |                |
| count       | int(11)              | NO   |     | 0       |                |
| updatetime  | int(11)              | NO   |     | NULL    |                |
| tids        | mediumtext           | NO   |     | NULL    |                |
| enable      | tinyint(1)           | NO   |     | -1      |                |
+-------------+----------------------+------+-----+---------+----------------+
10 rows in set (0.01 sec)1.censor.class1.php<?php/* censorStatus: {banned} {censor} {replace} */class Censor
{
  public $censorCount;
  public $censorData;
  public $censorLimit;  function __construct()
  {
    $this->censorCount = NULL;
    $this->censorCount->replace = 0;
    $this->censorCount->censor = 0;
    $this->censorCount->Banned = 0;
    $this->censorLimit = 120;
    $this->censorData = $this->GetCensorData();
  }  /*
  select * from
  */
  function GetCensorData()
  {
    static $censorData = NULL;
    if($censorData !== NULL) return $censorData;    $censorData->banned = null;
    $censorData->censor = null;
    $censorData->replace= null;
    $censorData->repalce->from = $censorData->replace->to = null;    global $dsql;
    $sql = "SELECT * FROM gk_info_censor WHERE replacement!='' AND `enable`=1 ";
    $dsql->Execute('s',$sql);
    while ($row = $dsql->GetArray('s'))
    {
      switch ($row['replacement'])
      {
        case '{banned}':  $censorData->banned[] = $row['find']; break;//banned
        case '{censor}':  $censorData->censor[] = $row['find']; break;//censor
        default:          $censorData->replace->from[] = $row['find'];//replace
                          $censorData->replace->to[] = $row['replacement'];
                          break;
      }
    }
    return $censorData;
  }  function check( &$string )
  {
    if($this->banned( $string) < 1)
    {
      if($this->censor( $string ) < 1)
      {
        $this->replace( $string );
      }
    }
  }  //替换级别 返回替换匹配个数
  function replace(&$subject)
  {
    if(empty($this->censorData->replace->from) || empty($this->censorData->replace->to)) return 0;    $i = 0;
    while ($arrFROM = array_slice($this->censorData->replace->find,$i,$this->censorLimit))
    {
      $i += $this->censorLimit;
      if(empty($arrFrom)) continue;
      $arrTo = array_slice($this->censorData->replace->to,$i,$this->censorLimit);
      #$subject = preg_replace($arrFrom, $arrTo, $subject);
      array_map('str_replace',$arrFrom,$arrTo,$subject);
    }
  }  //审核级别 返回审核匹配个数
  function censor(&$subject)
  {
    if(empty($this->censorData->censor)) return 0;    $i = $count = 0;
    while ($arr = array_slice($this->censorData->censor,$i,$this->censorLimit))
    {
      preg_match_all('#'.split('|',$arr).'#', $subject, $matches);
      if($matches) $count += count($matches[0]);
      $i += $this->censorLimit;
    }
    $this->censorCount->banned += $count;
    return $count;
  }  //彻底禁止 返回禁止匹配个数
  function banned(&$subject)
  {
    if(empty($this->censorData->banned)) return 0;    $i = $count = 0;
    while($arr = array_slice($this->censorData->banned,$i,$this->censorLimit))
    {
      preg_match_all('#'.split('|',$arr).'#', $subject, $matches);
      if($matches) $count += count($matches[0]);
      $i+= $this->censorLimit;
    }
    $this->censorCount->banned += $count;
    return $count;
  }  //审核级别 banned,censor,pass
  function censorLevel()
  {
    if($this->censorCount->banned > 0 ) return 'banned';
    if($this->censorCount->censor > 0 ) return 'censor';
    return 'pass';
  }
}2.censor.class2.php
<?php/* censorStatus: {banned} {censor} {replace} */class Censor
{
  public $censorCount_banned;
  public $censorCount_censor;
  public $censorCount_replace;
  public $censorData;
  public $censorLimit;
  public $cachePath;  function __construct()
  {
    $this->censorCount_replace = 0;
    $this->censorCount_censor = 0;
    $this->censorCount_banned = 0;
    $this->censorLimit = 120;
    $this->cachePath = DEDEDATA.'/cache_censor.php';
    $this->censorData = $this->GetCensorData();
  }
  function GetCensorData()
  {
    if(defined('CENSORDATA_LOADED'))
    {
      if(empty($censorData)) ShowMsg("ERROR:CENSOR数据不存在!");
      return $censorData;
    } elseif( file_exists($this->cachePath) && filesize($this->cachePath) > 1024) {
      include $this->cachePath;
      if(empty($this->cachePath)) ShowMsg('Error: censor cache file size error!');
      return $censorData;
    }    $censorData = array();
    $censorData['banned'] = array();
    $censorData['censor'] = array();
    $censorData['repalce']= array();    global $dsql;
    $sql = "SELECT * FROM gk_info_censor WHERE replacement!='' AND `enable`=1 ";
    $dsql->Execute('s',$sql);
    while ($row = $dsql->GetArray('s'))
    {
      switch ($row['replacement'])
      {
        case '{banned}':  $censorData[banned][] = $row['find']; break;//banned
        case '{censor}':  $censorData[censor][] = $row['find']; break;//censor
        default:          $censorData[replace][$row['find']]=$row['replacement'];break;//replace
      }
    }    #$cachePath = DEDEDATA.'/cache_censor.php';
    $cacheCode = "<"."?php\n";
    $cacheCode .= "\n#Make Time:".date("Y-m-d H:i:s");
    $cacheCode .= "\n#Make File:".__FILE__."(Line:".__LINE__.")";
    $cacheCode .= "\n#notice! do not modify me!\n\n";    $cacheCode .= "\ndefine('CENSORDATA_LOADED',TRUE);";
    $cacheCode .= "\n\$censorData=array();";
    $cacheCode .= "\n\$censorData[banned] = null;";
    $cacheCode .= "\n\$censorData[censor] = null;";
    $cacheCode .= "\n\$censorData[replace]= null;";    $cacheCode .= $this->makecode($censorData[banned],'censorData[banned]');
    $cacheCode .= $this->makecode($censorData[censor],'censorData[censor]');
    foreach ($censorData[replace] as $k => $v) {
      $cacheCode .= "\n\$censorData[replace]['".addslashes($k)."']= '".addslashes ($v)."';";
    }
    $cacheCode .= "\n\n?".">";    $fp = @fopen($this->cachePath,"w") or die(" Tag Engine Create File FALSE! ");
    fwrite($fp,$cacheCode);
    fclose($fp);
    return $censorData;
  }  function makecode($data,$varname)
  {
    $i=0;
    $codestring = '';
    while($arr = array_slice($data,$i,$this->censorLimit,true))
    {
      $codestring .= "\n\$".$varname."[] = '/". addcslashes( implode('|',$arr),"{}[]/.()'\"*" )."/i';";
      $i += $this->censorLimit;
    }
    return $codestring;
  }  function check( &$string )
  {
    $this->banned($string);
    $this->censor($string);
    $this->replace($string);
    /*
    if($this->banned( $string) < 1)
    {
      if($this->censor( $string ) < 1)
      {
        $this->replace( $string );
      }
    }
    */
  }  //替换级别 返回替换匹配个数
  function replace(&$subject)
  {
    if(empty($this->censorData[replace])) return 0;    foreach($this->censorData[replace] as $from => $to)
    {
      $subject = str_replace($from,$to,$subject);
    }
  }  //审核级别 返回审核匹配个数
  function censor(&$subject)
  {
    if(empty($this->censorData[censor])) return 0;    $count = 0;
    foreach($this->censorData[censor] as $pattern)
    {
      preg_match_all($pattern, $subject, $matches);
      if($matches) $count += count($matches[0]);
    }
    $this->censorCount_censor += $count;
    return $count;
  }  //彻底禁止 返回禁止匹配个数
  function banned(&$subject)
  {
    if(empty($this->censorData[banned])) return 0;    $count = 0;
    foreach($this->censorData[banned] as $pattern)
    {
      preg_match_all($pattern, $subject, $matches);
      if($matches) $count += count( $matches[0] );
      //print_r($matches);
    }
    $this->censorCount_banned += $count;
    return $count;
  }  //审核级别 banned,censor,pass
  function censorLevel()
  {
    if($this->censorCount_banned > 0 ) return 'banned';
    if($this->censorCount_censor > 0 ) return 'censor';
    return 'pass';
  }
}

解决方案 »

  1.   

    别的先不说
    至少第二段代码 29 行处的
          if(empty($censorData)) ShowMsg("ERROR:CENSOR数据不存在!");
          return $censorData;
    是错误的
      

  2.   

    上面那段代码用的步骤,不过还是有点问题,不是很好:<?php
    require_once DEDEINC.'/plugin/censor.class.php';
    $str = '提交的字符串';
    $censor = new Censor();
    $censor->check($str);
    switch($censor->censorLevel())
    {
      case 'banned': /*禁止发布*/   break;
      case 'censor': /*发布待审核*/ break;
      default:       /*直接发布*/   break;
    }
    是的,呵呵,这里报错。
      

  3.   

    个人觉得第二份比较好一点,不过还是有问题!有几个错的地方,另外preg_match_all 这个效率很低,反应速度很明显的慢~ 修改完之后如下2.censor.class2.php
    <?php/* censorStatus: {banned} {censor} {replace} */class Censor
    {
      public $censorCount_banned;
      public $censorCount_censor;
      public $censorCount_replace;
      public $censorData;
      public $censorLimit;
      public $cachePath;  function __construct()
      {
        $this->censorCount_replace = 0;
        $this->censorCount_censor = 0;
        $this->censorCount_banned = 0;
        $this->censorLimit = 120;
        $this->cachePath = DEDEDATA.'/cache_censor.php';
        $this->censorData = $this->GetCensorData();
      }
      function GetCensorData()
      {
        if(defined('CENSORDATA_LOADED') && !empty($GLOBALS['censorData']))
        {
          return $GLOBALS['censorData'];
        } elseif( file_exists($this->cachePath) && filesize($this->cachePath) > 1024) {
          include $this->cachePath;
          if(!empty($censorData)) return $censorData;
        }    $censorData = array();
        $censorData['banned'] = array();
        $censorData['censor'] = array();
        $censorData['repalce']= array();    global $dsql;
        $sql = "SELECT * FROM gk_info_censor WHERE replacement!='' AND `enable`=1 ";
        $dsql->Execute('s',$sql);
        while ($row = $dsql->GetArray('s'))
        {
          switch ($row['replacement'])
          {
            case '{banned}':  $censorData['banned'][] = $row['find']; break;//banned
            case '{censor}':  $censorData['censor'][] = $row['find']; break;//censor
            default:          $censorData['replace'][$row['find']]=$row['replacement'];break;//replace
          }
        }    #$cachePath = DEDEDATA.'/cache_censor.php';
        $cacheCode = "<"."?php\n";
        $cacheCode .= "\n#Make Time:".date("Y-m-d H:i:s");
        $cacheCode .= "\n#Make File:".__FILE__."(Line:".__LINE__.")";
        $cacheCode .= "\n#notice! do not modify me!\n\n";    $cacheCode .= "\ndefine('CENSORDATA_LOADED',TRUE);";
        $cacheCode .= "\n\$censorData=array();";
        $cacheCode .= "\n\$censorData['banned'] = null;";
        $cacheCode .= "\n\$censorData['censor'] = null;";
        $cacheCode .= "\n\$censorData['replace']= null;";    $cacheCode .= $this->makecode($censorData['banned'],"censorData['banned']");
        $cacheCode .= $this->makecode($censorData['censor'],"censorData['censor']");
        foreach ($censorData['replace'] as $k => $v) {
          $cacheCode .= "\n\$censorData['replace']['".addslashes($k)."']= '".addcslashes ($v,"{}[]/.()'\"*")."';";
        }
        $cacheCode .= "\n\n?".">";    $fp = @fopen($this->cachePath,"w") or die(" Tag Engine Create File FALSE! ");
        fwrite($fp,$cacheCode);
        fclose($fp);
        return $censorData;
      }  function makecode($data,$varname)
      {
        $i=0;
        $codestring = '';
        while($arr = array_slice($data,$i,$this->censorLimit,true))
        {
          $codestring .= "\n\$".$varname."[] = '/". addcslashes( implode('|',$arr),"{}[]/.()'\"*" )."/i';";
          $i += $this->censorLimit;
        }
        return $codestring;
      }  function check( &$string )
      {
        $this->banned($string);
        $this->censor($string);
        $this->replace($string);
        /*
        if($this->banned( $string) < 1)
        {
          if($this->censor( $string ) < 1)
          {
            $this->replace( $string );
          }
        }
        */
      }  //替换级别 返回替换匹配个数
      function replace(&$subject)
      {
        if(empty($this->censorData['replace'])) return 0;    foreach($this->censorData['replace'] as $from => $to)
        {
          $subject = str_replace($from,$to,$subject);
        }
      }  //审核级别 返回审核匹配个数
      function censor(&$subject)
      {
        if(empty($this->censorData['censor'])) return 0;    $count = 0;
        foreach($this->censorData['censor'] as $pattern)
        {
          preg_match_all($pattern, $subject, $matches);
          if($matches) $count += count($matches[0]);
        }
        $this->censorCount_censor += $count;
        return $count;
      }  //彻底禁止 返回禁止匹配个数
      function banned(&$subject)
      {
        if(empty($this->censorData['banned'])) return 0;    $count = 0;
        foreach($this->censorData['banned'] as $pattern)
        {
          preg_match_all($pattern, $subject, $matches);
          if($matches) $count += count( $matches[0] );
          //print_r($matches);
        }
        $this->censorCount_banned += $count;
        return $count;
      }  //审核级别 banned,censor,pass
      function censorLevel()
      {
        if($this->censorCount_banned > 0 ) return 'banned';
        if($this->censorCount_censor > 0 ) return 'censor';
        return 'pass';
      }
    }
      

  4.   


        $this->censorCount = NULL;
        $this->censorCount->replace = 0;
        $this->censorCount->censor = 0;
        $this->censorCount->Banned = 0;
    第一段代码 这种用法是自创的吧
      

  5.   

    如果非要选一个,那我可能会选第二个
    1. 第一个人滥用类,没申明的东西都开始加属性,这个习惯很不好,不知道他从哪学来的。
    2. if语句能写成这样 function check( &$string )
      {
        if($this->banned( $string) < 1)
        {
          if($this->censor( $string ) < 1)
          {
            $this->replace( $string );
          }
        }
      }
    3. 第二个人想到了将有用的经常调用的东西缓存起来总结一下:
    两人的代码看上去很牛X,实际上问题多多。楼主看一下哪个人态度好些吧,我觉的这个很重要
      

  6.   

    厉害!这都能够看出来,哈哈哈~
    谢谢点评。
    2. if语句能写成这样 function check( &$string )
      {
        if($this->banned( $string) < 1)
        {
          if($this->censor( $string ) < 1)
          {
            $this->replace( $string );
          }
        }
      }这个这里是指?
      

  7.   

    哈哈,讨论的人不多啊。今天画蛇添足,修改了一下~~感觉比原先完美多了~<?php/* censorStatus: {banned} {censor} {replace} */
    /* #database gk_info_censor,gk_info_censorlog #
    mysql> desc gk_info_censor;
    +-------------+----------------------+------+-----+---------+----------------+
    | Field       | Type                 | Null | Key | Default | Extra          |
    +-------------+----------------------+------+-----+---------+----------------+
    | id          | smallint(6) unsigned | NO   | PRI | NULL    | auto_increment |
    | admin       | varchar(15)          | NO   |     |         |                |
    | type        | smallint(6)          | NO   |     | 1       |                |
    | find        | varchar(255)         | NO   | UNI |         |                |
    | replacement | varchar(255)         | NO   |     |         |                |
    | extra       | varchar(255)         | NO   |     |         |                |
    | count       | int(11)              | NO   |     | 0       |                |
    | updatetime  | int(11)              | NO   |     | NULL    |                |
    | tids        | mediumtext           | NO   |     | NULL    |                |
    | enable      | tinyint(1)           | NO   |     | -1      |                |
    +-------------+----------------------+------+-----+---------+----------------+
    10 rows in set (0.02 sec)mysql> desc gk_info_censorlog;
    +-------------+-------------+------+-----+---------+----------------+
    | Field       | Type        | Null | Key | Default | Extra          |
    +-------------+-------------+------+-----+---------+----------------+
    | id          | int(11)     | NO   | PRI | NULL    | auto_increment |
    | tableid     | varchar(20) | NO   |     | NULL    |                |
    | dataid      | int(11)     | NO   |     | NULL    |                |
    | matchcensor | text        | NO   |     | NULL    |                |
    | matchbanned | text        | NO   |     | NULL    |                |
    | isdeal      | tinyint(1)  | NO   |     | -1      |                |
    +-------------+-------------+------+-----+---------+----------------+
    6 rows in set (0.00 sec)
    #how to use ?
    $str1 = 'post title';
    $str2 = 'post content';
    include DEDEINC.'/plugin/censor.class.php';
    $censor = new Censor();
    $censor->check($str1);
    $censor->check($str2);
    if($censor->censorLevel() == 'banned')
    {
      //your code
    } elseif ($censor->censorLevel() == 'censor') {
      //your code
    } else {
      //your code
    }
    */
    class Censor
    {
      public $censorMatch_banned;
      public $censorMatch_censor;
      public $censorData;
      public $censorLimit;
      public $cachePath;  function __construct()
      {
        $this->censorMatch_censor = array();
        $this->censorMatch_banned = array();    $this->censorLimit = 120;
        $this->cachePath = DEDEDATA.'/cache_censor.php';
        $this->censorData = $this->GetCensorData();
      }
      function GetCensorData()
      {
        if(defined('CENSORDATA_LOADED') && !empty($GLOBALS['censorData']))
        {
          return $GLOBALS['censorData'];
        } elseif( file_exists($this->cachePath) && filesize($this->cachePath) > 1024) {
          include $this->cachePath;
          if(!empty($censorData)) return $censorData;
        }    $censorData = array();
        $censorData['banned'] = array();
        $censorData['censor'] = array();
        $censorData['repalce']= array();    global $dsql;
        $sql = "SELECT * FROM gk_info_censor WHERE replacement!='' AND `enable`=1 ";
        $dsql->Execute('s',$sql);
        while ($row = $dsql->GetArray('s'))
        {
          switch ($row['replacement'])
          {
            case '{banned}':  $censorData['banned'][] = $row['find']; break;//banned
            case '{censor}':  $censorData['censor'][] = $row['find']; break;//censor
            default:          $censorData['replace'][$row['find']]=$row['replacement'];break;//replace
          }
        }    #$cachePath = DEDEDATA.'/cache_censor.php';
        $cacheCode = "<"."?php\n";
        $cacheCode .= "\n#Make Time:".date("Y-m-d H:i:s");
        $cacheCode .= "\n#Make File:".__FILE__."(Line:".__LINE__.")";
        $cacheCode .= "\n#notice! do not modify me!\n\n";    $cacheCode .= "\ndefine('CENSORDATA_LOADED',TRUE);";
        $cacheCode .= "\n\$censorData=array();";
        $cacheCode .= "\n\$censorData['banned'] = null;";
        $cacheCode .= "\n\$censorData['censor'] = null;";
        $cacheCode .= "\n\$censorData['replace']= null;";    $cacheCode .= $this->makecode($censorData['banned'],"censorData['banned']");
        $cacheCode .= $this->makecode($censorData['censor'],"censorData['censor']");
        foreach ($censorData['replace'] as $k => $v) {
          $cacheCode .= "\n\$censorData['replace']['".addslashes($k)."']= '".addcslashes ($v,"{}[]/.()'\"*")."';";
        }
        $cacheCode .= "\n\n?".">";    $fp = @fopen($this->cachePath,"w") or die(" Tag Engine Create File FALSE! ");
        fwrite($fp,$cacheCode);
        fclose($fp);
        return $censorData;
      }  function makecode($data,$varname)
      {
        $i=0;
        $codestring = '';
        while($arr = array_slice($data,$i,$this->censorLimit,true))
        {
          $codestring .= "\n\$".$varname."[] = '/". addcslashes( implode('|',$arr),"{}[]/.()'\"*" )."/i';";
          $i += $this->censorLimit;
        }
        return $codestring;
      }  function check( &$string )
      {
        $this->banned($string);
        $this->censor($string);
        $this->replace($string);
      }  //替换级别 返回替换匹配个数
      function replace(&$subject)
      {
        if(empty($this->censorData['replace'])) return 0;    foreach($this->censorData['replace'] as $from => $to)
        {
          $subject = str_replace($from,$to,$subject);
        }
      }  //审核级别 返回审核匹配个数
      function censor(&$subject)
      {
        if(empty($this->censorData['censor'])) return 0;    foreach($this->censorData['censor'] as $pattern)
        {
          preg_match_all($pattern, $subject, $matches);
          if($matches) $this->censorMatch_censor = array_merge($this->censorMatch_censor,$matches[0]);
        }
      }  //彻底禁止 返回禁止匹配个数
      function banned(&$subject)
      {
        if(empty($this->censorData['banned'])) return 0;    foreach($this->censorData['banned'] as $pattern)
        {
          preg_match_all($pattern, $subject, $matches);
          if($matches) $this->censorMatch_banned = array_merge($this->censorMatch_banned,$matches[0]);
        }
      }  //审核级别 banned,censor,pass
      function censorLevel()
      {
        if(count($this->censorMatch_banned) > 0 ) return 'banned';
        if(count($this->censorMatch_censor) > 0 ) return 'censor';
        return 'pass';
      }  //add sql log
      function insertLog($tableid,$dataid,$isdeal=-1)
      {
        if(empty($this->censorMatch_banned) && empty($this->censorMatch_censor)) return FALSE;
        global $dsql;
        $sql = "INSERT INTO gk_info_censorlog SET `tableid`='$tableid',`dataid`='$dataid',`isdeal`='$isdeal',";
        $sql .="`matchcensor`='". implode(',',$this->censorMatch_censor)."',";
        $sql .="`matchbanned`='". implode(',',$this->censorMatch_banned)."' ";
        return $dsql->ExecuteNoneQuery2( $sql );
      }
    }
      

  8.   

    SELECT * FROM gk_info_censor WHERE replacement!='' AND `enable`=1 " 这个语句就有待优化。
    结构很乱,看的很吃力。在不知功能的情况下可能还看不懂。
    代码不优美
      

  9.   


    //这一段建议包装一下。 如果你喜欢使用缓存策略,这块代码需要封装起来,便于重用!~
    //读和取,都封起来~
        if(defined('CENSORDATA_LOADED') && !empty($GLOBALS['censorData']))
        {
          return $GLOBALS['censorData'];
        } elseif( file_exists($this->cachePath) && filesize($this->cachePath) > 1024) {
          include $this->cachePath;
          if(!empty($censorData)) return $censorData;
        }    #$cachePath = DEDEDATA.'/cache_censor.php';
        $cacheCode = "<"."?php\n";
        $cacheCode .= "\n#Make Time:".date("Y-m-d H:i:s");
        $cacheCode .= "\n#Make File:".__FILE__."(Line:".__LINE__.")";
        $cacheCode .= "\n#notice! do not modify me!\n\n";
     
        $cacheCode .= "\ndefine('CENSORDATA_LOADED',TRUE);";
        $cacheCode .= "\n\$censorData=array();";
        $cacheCode .= "\n\$censorData['banned'] = null;";
        $cacheCode .= "\n\$censorData['censor'] = null;";
        $cacheCode .= "\n\$censorData['replace']= null;";
     
        $cacheCode .= $this->makecode($censorData['banned'],"censorData['banned']");
        $cacheCode .= $this->makecode($censorData['censor'],"censorData['censor']");
      

  10.   

    明显就是一个人写的,命名风格都一样。几点不足:1 “banned”这种常量字符串,在class中我更喜欢用const来声明。如果它们是枚举值,应该定义一个数组来存放全部枚举值,类似创建了一个哈希表,这样更加严谨。还有那些写死的数字,都应该使用const来定义,调用时self::XXX;这样改的时候直接从类的头部改一个地方下面全改了。2 虽然php没规定变量提前声明,但既然选择使用class,就应该规范一些。这样会造成一大堆警告。提前显示声明所有使用的变量,不要用隐式声明。3 完全是php4的class用法,没有封装的概念!所有方法全都默认的public,就没有private或protected。如果这个类是个单一功能类。那么应该只有一个public方法负责把最后结果返回给外部,外部只需要调用这一个方法就够了。其他的全部private,由这个方法在类内部去调用那些private或protected方法,如果此类以后仍有扩展的空间,需要继承的话,应该用protected。4 使用了全局变量,我认为在没有namespace的大多数情况下,都不应该使用全局变量,尤其是这种名称短的,什么$dsql,$GLOBALS['censorData']d的,尽量用参数传进来也不要用全局变量。5 $this->censorData[replace]这种极为初级的问题也有。不加引号php解释器会尝试把replace当成常量去解释,这种写法比加上引号效率低很多。6 $fp = @fopen($this->cachePath,"w") or die(" Tag Engine Create File FALSE! ");竟然还有这种代码就不能事先判断下file_exists并且is_writable下。非要用那个@符号。7 这代码好蛋疼。
    function check( &$string ) {
        if($this->banned( $string) < 1)
        {
          if($this->censor( $string ) < 1)
          {
            $this->replace( $string );
          }
        }
      }function banned(&$subject)
    对于这种引用传递的。可以使用链式语法,这些方法都不需要再返回那个数量,而是在方法内部做数量检查,然后都返回$this。
    然后这里就可以用链式语法了。function banned(&$subject){
        ...
        return $this;
    }function check( &$string ) {
        $this->banned( $string)
                ->censor( $string )
                ->replace( $string )
                ->
    }总结,对oop的理解很肤浅。除非用的是php4,否则这样使用class还不如直接写一堆函数。明显是为了用class而用的class,根本没有理解oop的精髓。有些方法可以拆开几个方法。虽然有些啰嗦,但那样才符合抽象,松耦合的概念。包括你改过之后的代码,仅仅是实现了功能而已,从oop的角度讲,还欠缺很多。