為什么mysql類(lèi)比Database 類(lèi)要先銷(xiāo)毀 在User__destruct()中執(zhí)行了sql 但是user類(lèi)先銷(xiāo)毀然后是mysql 而Database 沒(méi)有調(diào)用銷(xiāo)毀方法 按我的理解應(yīng)該是 先銷(xiāo)毀User 再銷(xiāo)毀Database 再銷(xiāo)毀mysql 斷開(kāi)連接應(yīng)該在mysql 的__destruct()中,但實(shí)際要放在Database 的__destruct()中
鏈接: http://pan.baidu.com/s/1qWlwEUK 密碼: 0lho ?這是跟老師學(xué)習(xí)寫(xiě)的全部示例代碼。
<?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(); 根據(jù)配置選擇不同的數(shù)據(jù)庫(kù)驅(qū)動(dòng)
? ? ? ? ? ?return self::$db;
? ? ? ?}
? ?}
? ?function where(){
? ? ? ?echo "調(diào)用了where \n";
? ? ? ?return $this;
? ?}
? ?function order(){
? ? ? ?echo "調(diào)用了order \n";
? ? ? ?return $this;
? ?}
? ?function limit(){
? ? ? ?echo "調(diào)用了limit \n";
? ? ? ?return $this;
? ?}
? ?function __destruct()
? ?{
? ? ? ?echo "Database封裝db被銷(xiāo)毀";
? ? ? ?$this->close(); //何時(shí)斷開(kāi)數(shù)據(jù)庫(kù)連接
? ?}
}
<?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執(zhí)行exec\n";
? ? ? ?return $res;
? ?}
? ?function close()
? ?{
? ? ? ?mysql_close($this->conn);
? ?}
? ?function __destruct()
? ?{
? ? ? ?//$this->close(); //此時(shí)斷開(kāi)數(shù)據(jù)庫(kù)連接不行
? ? ? ?echo "mysql被銷(xiāo)毀";
? ?}
}
<?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被銷(xiāo)毀";
? ?}
}
在做迭代模式示例時(shí)發(fā)現(xiàn):
為什么mysql類(lèi)比Database 類(lèi)要先銷(xiāo)毀
在User__destruct()中執(zhí)行了sql 但是user類(lèi)先銷(xiāo)毀然后是mysql ?而Database 沒(méi)有調(diào)用銷(xiāo)毀方法
按我的理解應(yīng)該是 先銷(xiāo)毀User ?再銷(xiāo)毀Database ? 再銷(xiāo)毀mysql ?斷開(kāi)連接應(yīng)該在mysql ?的__destruct()中,但實(shí)際要放在Database 的__destruct()中
2016-02-04
同學(xué)你好像沒(méi)有實(shí)例化Database類(lèi)哦,當(dāng)然不會(huì)調(diào)用析構(gòu)函數(shù)啦