当前位置:Gxlcms > PHP教程 > PHP框架一个方法调用的疑惑,贴出全部代码

PHP框架一个方法调用的疑惑,贴出全部代码

时间:2021-07-01 10:21:17 帮助过:26人阅读

请问class mysql 调用的_getInstance方法 在哪里定义的?贴出了全部代码

_db)){
            if(is_null($this->dbConfigKey)){
                $this->_db = Db::factor();
            }else{
                $this->_db = Db::factor($this->dbConfigKey);
            }
        }
        return $this->_db;
    }

    public function close(){
        $this->_getInstance()->close();
    }


    public function query($sql){
        return $this->_getInstance()->query($sql);
    }

    public function fetchAssoc($resource){
        return $this->_getInstance()->fetchAssoc($resource);
    }

    public function select($sql){
        return $this->_getInstance()->select($sql);
    }
}
_conn)){
            $this->_connect($dbConfigKey);
        }
    }

    private function _connect($dbConfigKey='DB_CONFIG'){
        $dbConfig = getConfig($dbConfigKey);
        $this->_conn = mysqli_connect($dbConfig['DB_HOST'], $dbConfig['DB_USERNAME'], $dbConfig['DB_PASSWORD'], $dbConfig['DB_NAME'], $dbConfig['DB_PORT']);
    }

    public function close(){
        mysqli_close($this->_getInstance());
    }

    public function query($sql){
        $result = mysqli_query($this->_conn, $sql);
        return $result;
    }

    public function fetchAssoc($resource){
        $rowList = array();
        while($row = mysqli_fetch_assoc($resource)){
            $rowList[] = $row;
        }
        return $rowList;
    }

    public function select($sql){
        $result = $this->query($sql);
        $rowList = $this->fetchAssoc($result);
        return $rowList;
    }
}

回复内容:

请问class mysql 调用的_getInstance方法 在哪里定义的?贴出了全部代码

_db)){
            if(is_null($this->dbConfigKey)){
                $this->_db = Db::factor();
            }else{
                $this->_db = Db::factor($this->dbConfigKey);
            }
        }
        return $this->_db;
    }

    public function close(){
        $this->_getInstance()->close();
    }


    public function query($sql){
        return $this->_getInstance()->query($sql);
    }

    public function fetchAssoc($resource){
        return $this->_getInstance()->fetchAssoc($resource);
    }

    public function select($sql){
        return $this->_getInstance()->select($sql);
    }
}
_conn)){
            $this->_connect($dbConfigKey);
        }
    }

    private function _connect($dbConfigKey='DB_CONFIG'){
        $dbConfig = getConfig($dbConfigKey);
        $this->_conn = mysqli_connect($dbConfig['DB_HOST'], $dbConfig['DB_USERNAME'], $dbConfig['DB_PASSWORD'], $dbConfig['DB_NAME'], $dbConfig['DB_PORT']);
    }

    public function close(){
        mysqli_close($this->_getInstance());
    }

    public function query($sql){
        $result = mysqli_query($this->_conn, $sql);
        return $result;
    }

    public function fetchAssoc($resource){
        $rowList = array();
        while($row = mysqli_fetch_assoc($resource)){
            $rowList[] = $row;
        }
        return $rowList;
    }

    public function select($sql){
        $result = $this->query($sql);
        $rowList = $this->fetchAssoc($result);
        return $rowList;
    }
}

提问者不够自信,采纳的答案更是把方向带偏了。
这就是个bug,没有人规定代码必须全对吧?
1-大家都知道有关闭数据库连接这个方法,但是实际开发中基本没有人会用,因为php自己会在进程结束时候关掉它。
2-从封装的方式也能看出来,这个叫lane的人通常通过model来操作mysql,而不是直接去调用mysql对象,而model中的close方法是对的,所以mysql中的close可能从来没有被调用过,因此这个错误一直没被发现。
3- @MaxFang 的评论很正确,从另一个侧面说明写代码的人水平并不高,所以这种错误就不难解释了。

综上,是bug,确实没定义。

之前一个答案我没看清楚就写了,发现居然可以删,机智的我。
谈一下我个人的看法,我感觉这两个类写的很不好,我不理解为什么Model里面要有数据库的基本操作方法,Model和Mysql实现同一个接口。。。如果这两部分代码都放在Mysql类中可能比较好理解,但是看具体的代码,我猜测这个是根据配置来支持不同的数据库,使用简单工厂模式就可以很好的实现这个功能吧,原来的代码感觉耦合度高,存在两个类的相互调用。

猜测可能是调用子类的,继承了Mysql的子类

问题没表述清楚啊

奇怪
看有没有达人来解释了

找下DbInterface接口

人气教程排行