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

时间:2016-08-30 01:23:43

标签: php mysql oop

我是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()?

OR

我是否错过了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();
在class item()中

并将其用于其余方法。

1 个答案:

答案 0 :(得分:2)

只需在构造函数中添加连接即可。添加必要的属性:

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类。您现在可以在db课程中使用db_conn的{​​{1}}媒体资源。

item

旁注:我已在最上面添加了准备好的陈述