2013-07-18 57 views
-2

我想創建一個簡單的示例類,然後開始在項目中實現以下內容,並且想知道是否有某些我可以/應該改進的東西。如果有人給我一些我現在正在做的事情,我會很高興。面向對象的PHP + PDO - 這是應該如何?

簡單類的例子:

// define class 
    class User{ 
    private $UserID; 
    private $UserName; 
    private $Email; 

    function SetUserID($NewUserID){ 
    $this -> UserID = $NewUserID; 
    } 

    function GetUserID(){ 
    return $this -> UserID; 
    } 

    function SetUserName($NewUserName){ 
    // update object 
    $this -> UserName = $NewUserName; 
    } 

    function GetUserName(){ 
    return $this -> UserName; 
    } 

    function SetEmail($NewEmail){ 
    $this -> Email = $NewEmail;   
    } 

    function GetEmail(){ 
    return $this -> Email; 
    } 

    public function SaveUser(){ 
    // check if user exists 
    $User = new User(); 
    if ($User->UserExists($this->UserID)){ 
     // user exists - update him 
     $Pdo = new PDO('mysql:host=localhost;dbname=cms;charset=utf8', 'xxx', 'xxx'); 
     $Query = $Pdo->prepare("Update User set UserName = :UserName, Email = :Email, UserID = :UserID where UserID = :UserID"); 
     $Query->bindValue(':UserName', $this->UserName); 
     $Query->bindValue(':Email', $this->Email); 
     $Query->bindValue('UserID', $this->UserID); 
     $Query->execute(); 
     $Pro = null; 
     } 
    else{ 
     // insert new 
     $Pdo = new PDO('mysql:host=localhost;dbname=cms;charset=utf8', 'xxx', 'xxx'); 
     $Query = $Pdo->prepare("Insert into User (UserName, Email) values (:UserName, :Email)"); 
     $Query->bindValue(':UserName', $this->UserName); 
     $Query->bindValue(':Email', $this->Email); 
     $Query->execute(); 
     // close connection 
     $Pdo = null; 
     } 
    } 

    private function UserExists($UserID){ 
    // returns true if users exists, false if not 
    $Pdo = new PDO('mysql:host=localhost;dbname=cms;charset=utf8', 'root', 'vertrigo'); 
    $Query = $Pdo->prepare("SELECT * FROM User WHERE UserID=:UserID"); 
    $Query->bindValue(':UserID', $UserID); 
    $Query->execute(); 
    $Row = $Query->fetch(PDO::FETCH_ASSOC); 
    $Pdo = null; 

    // get user 
    if ($Row){ 
     return true; 
     } 
    else{ 
     return false; 
     } 
    }   
    } 

function GetUserInfo($UserID){ 
    // open pdo 
    $Pdo = new PDO('mysql:host=localhost;dbname=cms;charset=utf8', 'root', 'vertrigo'); 
    $Query = $Pdo->prepare("SELECT * FROM User WHERE UserID=:UserID"); 
    $Query->bindValue(':UserID', $UserID); 
    $Query->execute(); 
    $Row = $Query->fetch(PDO::FETCH_ASSOC); 
    $Pdo = null; 

    // get UserInfo 
    $UserInfo = new User(); 
    $UserInfo -> SetUserID($UserID); 
    $UserInfo -> SetUserName($Row["UserName"]); 
    $UserInfo -> SetEmail($Row["Email"]); 

    // return UserInfo 
    return $UserInfo; 
    } 

這是我想如何與它的工作只是一個簡單的例子。你認爲我應該改進什麼?

+1

只是好奇,你是否嘗試瀏覽其他類似的問題? –

+1

這更適合[programmers.stackexchange.com](http://programmers.stackexchange.com/),它是一般代碼改進問題。 –

+1

你正在改變數據庫密碼真的很難... –

回答

0

任何PHP應用程序只能連接到數據庫一次
因此,您的課程必須使用已經創建的連接,而不是每次創建連接。

class User{ 
    private $UserID; 
    private $UserName; 
    private $Email; 
    private $db; 

    function __construct($pdo) { 
    $this->db = $pdo; 
    } 

    private function UserExists($UserID) 
    { 
    $sql = "SELECT 1 FROM User WHERE UserID=?"; 
    $stm = $this->db->prepare($sql); 
    $stm->execute(array($UserID)); 
    return $stm->fetchColumn(); 
    }   
    } 
    // the rest going to use the same approach 
} 

$pdo = //see PDO tag wiki for theproper example 

$user = new User($pdo); 
if ($user->UserExists($UserID)) { 
    // whatever 
} 
+0

謝謝你的例子。在這種情況下 - 你會在「新用戶($ pdo)中傳遞什麼?我的意思是 - 你必須創建一個新的pdo對象繼續嗎?謝謝你的幫助:-) – Enn

+0

你必須創建一個新的對象pdo當你開始你的應用程序時,並且一直使用這個對象,不僅對於這個特定的類,而且對於所有的數據庫交互,不管你是否使用come類。 –