我是 php OOP 的新手。我有一个关于我的情况的问题。
db.php
class db{
protected $db_host;
protected $db_name;
protected $db_user_name;
protected $db_pass;
public function __construct() {
$this->db_host="localhost";
$this->db_name="bs";
$this->db_user_name="root";
$this->db_pass="";
}
public function conn(){
try {
$conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user_name="root", $this->db_pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
}
item.php
require "../../includes/db.php";
class item{
public $user_uid;
public $last_id;
public $display_item;
public $iid;
protected $item_id;
protected $item_name;
protected $item_price;
protected $item_quantity;
public function add_item($uid,$item_id,$item_n,$item_p,$item_q){
$db = new db();
$conn=$db->conn();
$this->user_uid=$uid;
$this->item_id=$item_id;
$this->item_name=$item_n;
$this->item_price=$item_p;
$this->item_quantity=$item_q;
try {
$sql = "INSERT item(uid,item_id,item_name,item_price,item_quantity)
VALUES('$this->user_uid','$this->item_id','$this->item_name','$this->item_price','$this->item_quantity')";
$conn->exec($sql);
$this->last_id=$conn->lastInsertId();
}
catch(PDOException $e){
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
public function display_item($uid){
$db = new db();
$conn=$db->conn();
$this->user_uid=$uid;
try{
$sql="SELECT * FROM item where uid='$this->user_uid'";
$statement=$conn->query($sql);
while($row=$statement->fetch()){
$this->iid[]=$row['iid'];
$this->display_item[]=[$row['item_id'],$row['item_name'],$row['item_price'],$row['item_quantity']];
}
}
catch(PDOException $e){
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
}
正如您从 item.php 中看到的,我必须调用
$db = new db();
$conn=$db->conn();
在 add_item() 和 display_item() 中。
所以这意味着我必须访问数据库连接的每个方法,我都必须这样做。有没有更简单的方法通过修改我的代码不在其他类中创建 $db = new db() 来做到这一点?
或
我是否遗漏了一些 PHP 可以做到的功能?
我想要的方式
require "../../includes/db.php";
$db = new db();
$conn=$db->conn();
class item{
...
...
public function add_item($uid,$item_id,$item_n,$item_p,$item_q){
try {
$sql = "INSERT item(uid,item_id,item_name,item_price,item_quantity)
VALUES('$this->user_uid','$this->item_id','$this->item_name','$this->item_price','$this->item_quantity')";
$conn->exec($sql);
}
public function display_item($uid){
....
try{
$sql="SELECT * FROM item where uid='$this->user_uid'";
$statement=$conn->query($sql);
所以我只声明 1 次()
$db = new db();
$conn=$db->conn();
在类 item() 中并将其用于其余方法。
最佳答案
只需在构造函数中添加连接即可。还要添加必要的属性:
class db
{
protected $db_host;
protected $db_name;
protected $db_user_name;
protected $db_pass;
protected $db_conn; // add me here
然后在构造函数中,使用那里的凭据进行连接。因此,当您创建对象时,它已经连接,您只需使用属性连接来执行查询和内容:
public function __construct()
{
$this->db_host = "localhost";
$this->db_name = "bs";
$this->db_user_name = "root";
$this->db_pass = "";
// connect
try {
$this->db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user_name="root", $this->db_pass);
$this->db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db_conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
exit;
}
}
然后,扩展item中的db
类。您现在可以在 item
类中使用 db
的 db_conn
属性。
class item extends db
{
public $user_uid;
public $last_id;
public $display_item;
public $iid;
protected $item_id;
protected $item_name;
protected $item_price;
protected $item_quantity;
public function display_item($uid)
{
try {
$stmt = $this->db_conn->prepare('SELECT * FROM item WHERE uid = :uid');
$stmt->bindValue(':uid', $uid);
$stmt->execute();
foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$this->iid[] = $row['iid'];
$this->display_item[] = [$row['item_id'], $row['item_name'], $row['item_price'], $row['item_quantity']];
}
} catch(PDOException $e) {
echo $e->getMessage();
exit;
}
}
}
旁注:我在顶部添加了准备好的语句
关于PHP oop 如何以更简单的方式调用另一个类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39217483/