_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接口