【问题标题】:OOP PHP + PDO - Is this how it should be?OOP PHP + PDO - 应该是这样吗?
【发布时间】:2013-07-17 03:42:07
【问题描述】:

在我开始在我的项目中实施以下内容之前,我正在尝试创建一个简单的示例类,我想知道是否有一些我可以/应该改进的地方。如果有人给我一些关于我现在做得如何的意见,我会很高兴。

简单的类示例:

  // 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;
  }

这只是我想如何使用它的一个基本示例。你觉得我应该改进什么?

【问题讨论】:

  • 只是好奇,您是否尝试浏览其他类似问题?
  • 这更适合programmers.stackexchange.com,用于一般代码改进问题。
  • 你让更改数据库密码变得非常困难......
  • 当然有,我也看过好几本不同的教程,我不知道哪个是“最好的”。
  • 每次必须运行查询时,您从什么答案中得到连接的想法?可以给个链接吗?

标签: php class oop pdo


【解决方案1】:

任何 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
}

【讨论】:

  • 谢谢你的例子。在这种情况下 - 你会在 "New User($pdo) 中传递什么?我的意思是 - 你必须创建一个新的 pdo 对象才能继续?谢谢你的帮助:-)
  • 您必须在启动应用程序时创建一个新的 pdo 对象。并一直使用该对象。不仅适用于这个特定的类,而且适用于所有数据库交互。不管你是否使用来类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
相关资源
最近更新 更多