繁体   English   中英

PHP oop如何以更简单的方式调用另一个类方法?

[英]PHP oop how to call another class method in a easier way?

我是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类中使用dbdb_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;
        }
    }
}

旁注:我在顶部添加了准备好的语句

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM