為什么mysql類比Database 類要先銷毀 在User__destruct()中執行了sql 但是user類先銷毀然后是mysql 而Database 沒有調用銷毀方法 按我的理解應該是 先銷毀User 再銷毀Database 再銷毀mysql 斷開連接應該在mysql 的__destruct()中,但實際要放在Database 的__destruct()中
鏈接: http://pan.baidu.com/s/1qWlwEUK 密碼: 0lho ?這是跟老師學習寫的全部示例代碼。
<?php
?
namespace Com;
class Database {
? ?static private $db ;
? ?private function __construct() //單例模式 ?只能new一次mysql
? ?{
? ?}
? ?static function getInstance()
? ?{
? ? ? ?if(self::$db){
? ? ? ? ? ?return self::$db;
? ? ? ?}else{
? ? ? ? ?// ?self::$db = new Database();
? ? ? ? ? ?self::$db ? = new \Com\db\Mysql(); //$dbtest ?= new \Com\db\Mysqli(); 根據配置選擇不同的數據庫驅動
? ? ? ? ? ?return self::$db;
? ? ? ?}
? ?}
? ?function where(){
? ? ? ?echo "調用了where \n";
? ? ? ?return $this;
? ?}
? ?function order(){
? ? ? ?echo "調用了order \n";
? ? ? ?return $this;
? ?}
? ?function limit(){
? ? ? ?echo "調用了limit \n";
? ? ? ?return $this;
? ?}
? ?function __destruct()
? ?{
? ? ? ?echo "Database封裝db被銷毀";
? ? ? ?$this->close(); //何時斷開數據庫連接
? ?}
}
<?php
/**
* Created by PhpStorm.
* User: dxx
* Date: 2015/1/27
* Time: 14:34
*/
namespace com\db;
use Com\IDb;
class Mysql implements IDb{
? ?private $conn ;
? ?function __construct()
? ?{
? ? ? ?$this->connect('127.0.0.1','root','dxxDE0929','fly');
? ?}
? ?function connect($host,$user,$sec,$dbname)
? ?{
? ? ? ?$conn = mysql_connect($host,$user,$sec);
? ? ? ?mysql_select_db($dbname,$conn);
? ? ? ?$this->conn = $conn;
? ?}
? ?function query($sql)
? ?{
? ? ? ?$res = ?mysql_query($sql);
? ? ? ?$resarr = '';
? ? ? ?while ($line = mysql_fetch_array($res, MYSQL_ASSOC)) {
? ? ? ? ? ?$resarr[] = $line;
? ? ? ?}
? ? ? ?mysql_free_result($res);
? ? ? ?return $resarr;
? ?}
? ?function exec($sql)
? ?{
? ? ? ?$res = ?mysql_query($sql);
? ? ? ?echo "\n執行exec\n";
? ? ? ?return $res;
? ?}
? ?function close()
? ?{
? ? ? ?mysql_close($this->conn);
? ?}
? ?function __destruct()
? ?{
? ? ? ?//$this->close(); //此時斷開數據庫連接不行
? ? ? ?echo "mysql被銷毀";
? ?}
}
<?php
?
namespace com\model;
use Com\Factory;
class User
{
? ?public $uid ;
? ?public $username ;
? ?public $password ;
? ?public $realname ;
? ?private $tablename ;
? ?private $record;
? ?private $db;
? ?function __construct($id)
? ?{
? ? ? ?$this->tablename = 'd_user';
? ? ? ?$this->db = Factory::createDb();
? ? ? ?$res = $this->db->query("select * from {$this->tablename} where uid={$id} limit 1");
? ? ?// print_r($res);exit();
? ? ? ?$this->record = $res[0] ;
? ? ? ?$this->uid ?= $id;
? ? ? ?$this->username = $res[0]['username'];
? ? ? ?$this->password = $res[0]['password'];
? ? ? ?$this->realname = $res[0]['realname'];
? ?}
? ?function __destruct()
? ?{
? ? ? ?$res = $this->db->exec("update {$this->tablename} set username='{$this->username}', realname='{$this->realname}'
? ? ? ? ? ? ? ? ? ? ? ? ?where uid={$this->uid} limit 1");
? ? ? ?echo "\n \n".$this->uid ."User被銷毀";
? ?}
}
在做迭代模式示例時發現:
為什么mysql類比Database 類要先銷毀
在User__destruct()中執行了sql 但是user類先銷毀然后是mysql ?而Database 沒有調用銷毀方法
按我的理解應該是 先銷毀User ?再銷毀Database ? 再銷毀mysql ?斷開連接應該在mysql ?的__destruct()中,但實際要放在Database 的__destruct()中
2016-02-04
同學你好像沒有實例化Database類哦,當然不會調用析構函數啦