請問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介面