PHP架構一個方法調用的疑惑,貼出全部代碼

來源:互聯網
上載者:User
關鍵字 php 架構 php架構
請問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介面

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.