Home > Backend Development > PHP Tutorial > PHP框架一个方法调用的疑惑,贴出全部代码

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

WBOY
Release: 2016-06-06 20:10:51
Original
884 people have browsed it

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

<code><?php namespace LaneSmartFW\DB;
/**
 * 基础Model类,所有的Model文件均继承本类
 * Created by lixuan-it@360.cn
 * User: lane
 * Date: 15/8/27
 * Time: 下午6:35
 * E-mail: lixuan868686@163.com
 * WebSite: http://www.lanecn.com
 */
class Model implements DbInterface {

    protected $dbConfigKey = null;

    private $_db = null;

    private function _getInstance(){
        if(is_null($this->_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);
    }
}</code>
Copy after login
Copy after login
<code><?php namespace LaneSmartFW\DB;

/**
 * Created by lixuan-it@360.cn
 * User: lane
 * Date: 15/8/27
 * Time: 下午3:29
 * E-mail: lixuan868686@163.com
 * WebSite: http://www.lanecn.com
 */
class Mysql implements DbInterface{

    private $_conn = null;

    public function __construct($dbConfigKey='DB_CONFIG'){
        if(is_null($this->_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;
    }
}</code>
Copy after login
Copy after login

回复内容:

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

<code><?php namespace LaneSmartFW\DB;
/**
 * 基础Model类,所有的Model文件均继承本类
 * Created by lixuan-it@360.cn
 * User: lane
 * Date: 15/8/27
 * Time: 下午6:35
 * E-mail: lixuan868686@163.com
 * WebSite: http://www.lanecn.com
 */
class Model implements DbInterface {

    protected $dbConfigKey = null;

    private $_db = null;

    private function _getInstance(){
        if(is_null($this->_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);
    }
}</code>
Copy after login
Copy after login
<code><?php namespace LaneSmartFW\DB;

/**
 * Created by lixuan-it@360.cn
 * User: lane
 * Date: 15/8/27
 * Time: 下午3:29
 * E-mail: lixuan868686@163.com
 * WebSite: http://www.lanecn.com
 */
class Mysql implements DbInterface{

    private $_conn = null;

    public function __construct($dbConfigKey='DB_CONFIG'){
        if(is_null($this->_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;
    }
}</code>
Copy after login
Copy after login

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

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

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

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

问题没表述清楚啊

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

找下DbInterface接口

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template