2011-09-17 77 views
0

嗯,我試圖擴展名爲數據庫的繼承,它可以輕鬆地檢索數據庫信息(如果正確地完成一行)的類mysqli。它不工作了,到目前爲止,因爲我收到以下錯誤:mysqli及其繼承錯誤

這裏所定義的數據庫類:

class Database extends mysqli{ 
    private $select, $create, $insert, $alter, $update, $delete, $drop; 
    protected $mysql, $result, $table, $column, $where, $value, $limit, $order, $type; 

    public function __construct($host, $user, $pass, $db){ 
    $this->mysql = new mysqli($host, $user, $pass, $db) 
      or die("Error connecting to the database {$db}"); 

    } 

    public function __destruct(){ 
    $this->mysql->close(); 
    } 

    protected function prepareQuery(){ 
    if ($prepare = $this->mysqli->prepare($this->query)) { 
     trigger_error("Problem preparing query ($this->query) ".$this->mysqli->error, E_USER_ERROR); 
    } 
    return $prepare; 
    } 

    protected function reset(){ 
    unset($this->table); 
    unset($this->column); 
    unset($this->where); 
    unset($this->value); 
    unset($this->limit); 
    unset($this->order); 
    } 

    public function select($column){  
    $this->select = implode(",", $column); 
    return $this; 
    } 

    public function table($table){ 
    $this->table = $table; 
    return $this; 
    } 

    public function where($where, $comparison = "=", $logic){ 
    $i = 0; 
    foreach ($where as $col => $val){ 
     $wherestring .= (is_array($comparison)) ? " {$col} {$comparison[$i]} '{$val}'" : " WHERE {$col} {$comparison} '{$val}'"; 
     $wherestring .= ($i < (count($where)-1))?" {$logic[$i]}" :" "; 
     $i++; 
    } 
    $this->where = $wherestring; 
    return $this; 
    } 

    public function limit($limit){ 
    $this->limit = $limit; 
    return $this; 
    } 

    public function order($order){ 
    $this->order = $order; 
    return $this; 
    } 

    public function runquery($method){ 
    $query = "{$method} {$this->select} FROM {$this->table}"; 
    if(!empty($this->where)) $query .= " WHERE {$this->where}"; 
    if(!empty($this->limit)) $query .= " LIMIT {$this->limit}"; 
    if(!empty($this->order)) $query .= " ORDER BY {$this->order}"; 
    echo "The generated Query is: \n".$query; 
    $this->result = parent::query($query); 
    $result = parent::fetch_array($this->result); 
    return $result; 
    } 

} 

這是我從一個腳本文件,運行方式:

include("inc/config.php"); 
include("classes/class_data.php"); 

$db = new Database($dbhost, $dbuser, $dbpass, $dbname); 
$row = $db->select(array("password","email"))->table($prefix."users")->where(array("uid"=>1, "username"=>Admin"),array("=","="),array("AND"))->limit(2)->order("uid")->runquery("SELECT"); 

它沒有工作,我得到了下面的警告和錯誤消息: 代碼:

Warning: mysqli::query() [mysqli.query]: Couldn't fetch Database in classes/class_data.php on line 70 
Fatal error: Call to undefined method mysqli::fetch_array() inclass_data.php on line 71 

我現在有點困惑,因爲我似乎找不到解決這個問題的方法。你能幫助任何人嗎?我非常感激。

謝謝

回答

0

fecth_array()不是mysqli的方法,但MySQLi_STMT類。

而且你不應該設計這樣的,你正在做的事情,extend mysqli,但你也做$this->mysql = new mysqli(...)is-Ahas-A之間,你應該選擇一個。我建議has-A

0

除了說什麼xdazz,你也「繼承自」混合和「使用」的模式:你有一個mysqli的實例名爲$this->mysql以及$this,從parent:: mysqli的繼承。你應該選擇一個,但不要同時使用兩個。如果你希望你的類的用戶能夠對它做任何事情,她可以用一個普通的mysqli實例(包括自己調用所有的超類方法),使用繼承。如果要限制用例,請使用mysqli類型的實例變量並刪除繼承。然後,您可以有選擇地代理內部$this->mysql的方法,以允許用戶調用它們。

+0

哦,我的答案已經被取代,xdazz現在也在說這個... –