放牧代码和思想
专注自然语言处理、机器学习算法
    愛しさ 優しさ すべて投げ出してもいい

BAE移植版Discuz解读某处错误

不知道是哪位放出的Discuz_X2.5_SC_UTF8 for BAE1.4,我仔细地看了他的代码,有几处不敢苟同。就拿今天看的一部分来讲吧,这位先生自己封装了一个类来操作BCS,在bcs\class_file.php中,这位先生写了这么一段代码:

public function add_remote_dir($dirname) {
    $dirname = $this->get_full_path($dirname);
    if (array_key_exists($dirname, $this->remotedirs)) {
        return;
    }
    array_push($this->remotedirs, $dirname, $dirname);
    krumo($this->remotedirs);
}

(最后一句krumo是我调试用的)这位爷似乎没搞明白array_key_exists的用法,array_key_exists是判断一个数组中是否存在键名,也就是只搜索键不搜索值的,所以这个if条件永远不可能为真。这位爷似乎又有点迷糊自己的条件怎么一直不为真,所以接着用array_push连着push了两个相同的字符串进去,他可能以为前面的一个能变成键,后面一个能变成值吧。可以这又不对了,array_push根本没有把一个线性数组变成键值数组的功能。所以搞来搞去还是一场空:


上图数组的键值是默认的0123,不是路径名。

好在这位爷也没有赋予这个数组多大的使命,顶多写了一句unset($this->remotedirs[$dirname]);,看来作者的心里这个数组还是二维数组呀。

顺手放个顺着作者思维修改的:

public function add_remote_dir($dirname) {
        $dirname = $this->get_full_path($dirname);
        if (in_array($dirname, $this->remotedirs)) {
            return;
        }
        array_push($this->remotedirs, $dirname);
        krumo($this->remotedirs);
    }
 
    public function remove_remote_dir($dirname) {
        $dirname = $this->get_full_path($dirname);
        foreach ($this->remotedirs as $key => $val)
        {
            if ($val == $dirname)
            {
                unset($this->remotedirs[$key]);
            }
        }
    }
 
    public function add_remote_file($filename) {
        $filename = $this->get_full_path($filename);
        if (in_array($filename, $this->remotefiles)) {
            return;
        }
        array_push($this->remotefiles, $filename);
    }
 
    public function remove_remote_file($filename) {
        $filename = $this->get_full_path($filename);
        foreach ($this->remotedirs as $key => $val)
        {
            if ($val == $filename)
            {
                unset($this->remotedirs[$key]);
            }
        }
    }

作者移植BAE一定花了很多功夫,在此表示感谢!

知识共享许可协议 知识共享署名-非商业性使用-相同方式共享码农场 » BAE移植版Discuz解读某处错误

评论 欢迎留言

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

我的作品

HanLP自然语言处理包《自然语言处理入门》