2012-07-16 35 views
4

我有點新的數據庫連接使用MySQL的PDO,這裏有我的兩個文件:PHP PDO MySQL的:如何使用在不同類

我有我用來連接到數據庫的連接類:

class connection{ 

private $host = 'localhost'; 
private $dbname = 'devac'; 
private $username = 'root'; 
private $password =''; 

public $con = ''; 

function __construct(){ 

    $this->connect(); 

} 

function connect(){ 

    try{ 

     $this->con = new PDO("mysql:host=$this->host;dbname=$this->dbname",$this->username, $this->password); 
     $this->con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); 


    }catch(PDOException $e){ 

     echo 'We\'re sorry but there was an error while trying to connect to the database'; 
     file_put_contents('connection.errors.txt', $e->getMessage().PHP_EOL,FILE_APPEND); 

    } 
} 
} 

我有我使用從數據庫中查詢數據的ACCOUNT_INFO類:

class account_info{ 


function getAccountInfo(){ 

    $acc_info = $this->con->prepare("SELECT * FROM account_info"); 
    $acc_info->execute(); 

    $results = $acc_info->fetchAll(PDO::FETCH_OBJ); 

    foreach ($results as $key) { 
     $results->owner_firstname; 

    } 
}  


} 

我包括在我的index.php頁面這些文件:

include_once 'classes/connection.class.php'; 
include_once 'classes/accountinfo.class.php'; 

$con = new connection(); 
$info = new account_info(); 
$info->getAccountInfo(); 

我只是不能得到它的工作我沒有得到任何輸出,我認爲它與範圍有關,但我不知道正確的原因爲什麼要解決它,因爲我是新手這PDO和麪向對象的東西。 在此先感謝。

回答

10

解決方案1 ​​

更換class account_info {class account_info extends connection {

更換

$con = new connection(); 
$info = new account_info(); 

$info = new account_info(); 

,它應該工作。

方案2(建議)

我強烈建議你用在這種情況下,依賴注入解決您的問題。 只是更換您的賬戶類:

class account_info { 

    private $con; 

    public function __construct(connection $con) { 
     $this->con = $con->con; 
    } 

    public function getAccountInfo(){ 

     $acc_info = $this->con->prepare("SELECT * FROM account_info"); 
     $acc_info->execute(); 

     $results = $acc_info->fetchAll(PDO::FETCH_OBJ); 

     foreach ($results as $key) { 
      $results->owner_firstname; 
     } 
    }  

} 

,並用它在index.php文件是這樣的:

include_once 'classes/connection.class.php'; 
include_once 'classes/accountinfo.class.php'; 

$con = new connection(); 
$info = new account_info($con); 
$info->getAccountInfo(); 

說明

作爲一般好的規則:始終指定範圍的關鍵字功能(公共,受保護或私人)。

第一種解決方案稱爲繼承,我們基本上做的是用連接類擴展帳戶類,以便繼承連接類中的所有方法和屬性並輕鬆使用它們。在這種情況下,你必須小心命名衝突。我建議你看看PHP手冊中的類繼承。

第二種解決方案稱爲依賴注入,它是一種極受鼓勵的設計模式,它使您的類在其構造函數中接受其他類,以顯式定義類依賴關係樹(在這種情況下,帳戶依賴於連接並且沒有連接我們不能讓帳戶工作)。

數以千計的可能的解決方案的另一個,將有人發佈下面是一種稱爲Singleton的設計模式。然而這個模式最近被重新評估爲反模式,不應該使用。

+0

然後他將爲他的賬戶類的每個實例創建一個新的數據庫連接(以及其他每個類這需要數據庫連接)。 – Martin 2012-07-16 23:49:59

+0

@Martin,我已經更新了我的答案 – Shoe 2012-07-16 23:58:15

+0

好了,所以下面的最新版本,他現在需要跟蹤$ con在整個應用程序範圍內(並且非常小心,不要覆蓋它),並將其傳遞給每個其他對象/明確地關閉。現在我明白爲什麼單身人士是一個壞主意! :) – Martin 2012-07-16 23:58:43

7

常用的方法是在數據庫類中使用singleton模式。

事情是這樣的:

class connection { 

    private static $hInstance; 

    public static function getInstance() { 
    if (!(self::$hInstance instanceof self)) { 
     self::$hInstance = new self(); 
    } 

    return self::$hInstance; 
    } 

    /* your code */ 
} 

然後,您可以簡單地使用

$database = connection::getInstance(); 
$database->con->prepare(....) 

+0

現在單身人士非常氣餒。 – Shoe 2012-07-16 23:47:17

+0

小心引用來源? – Martin 2012-07-16 23:49:06

+0

http://c2.com/cgi/wiki?SingletonsAreEvil – Shoe 2012-07-16 23:59:06