【问题标题】:OOP PHP integrate mysqli_queryOOP PHP 集成 mysqli_query
【发布时间】:2013-10-18 02:33:09
【问题描述】:

我将编辑问题以使其更清晰,这样您就可以看到我现在得到了什么,并更容易理解问题。

<?php
$mysqli = new mysqli("localhost", "user", "password", "test");
class building
{
private $mysqli;
public $buildingid;
public $userid;
public $buildinglevel;




public function __construct($buildingid, $userid, \mysqli $mysqli)
{
    $this->buildinglevel;
    $this->mysqli = $mysqli;
}

public function getLevel()
{
    return $this->mysqli->query("SELECT ".$this->buildingid." FROM worlds WHERE city_userid=".$this->userid."");
}
}

}
?>

那我用这个来创建和使用函数:

$cityHall = new building("cityHall",$user['id'],$mysqli);
echo $cityHall->getLevel();

这变成了空白,什么也没有发生。

【问题讨论】:

  • 在构造函数中做,这就是它的目的。
  • 我尝试将它添加到构造函数中,但我不想每次都重新定义 $mysqli 的值,似乎浪费时间?所以基本上,我怎样才能将 $mysqli 保留在 Class 之外并导入它,以便能够在构造函数内部执行查询?
  • 询问代码的问题应包括尝试过的解决方案、这些解决方案做了什么以及您预期会发生什么。 (又名“某人必须对某个问题有最低限度的了解才能在 SO 上提出问题”)
  • @Narf 虽然您可以在构造函数中进行计算,但这是一个糟糕的想法,几乎不可能进行单元测试。构造函数主要用于初始化类状态(即注入适当的依赖项)

标签: php mysql oop mysqli integrate


【解决方案1】:

对象是封装通过方法暴露给其他对象的行为的单元。从数据库中检索到的公共属性的包装器不是面向对象编程的。事实上,mysqli 可以通过fetch_object 为您做到这一点:

$result = $mysqli->query($query);
while ($building = $result->fetch_object()) {
    // access properties via $building->buildingid, etc.
}

除非building 类实际上通过方法提供了功能,并实现了一些抽象,否则它是不需要的。相反,您可以拥有一个 DAO(数据访问对象),它包装了 DB (mysqli),并且它检索到的数据供您的模型使用。

interface Dao {
    public function get($id);
}

class BuildingDao implements Dao {
    // DB can be a wrapper for mysqli
    // but servers as an interface so it
    // can be replaced easily
    public function __construct(DB $db) {
        $this->db = $db;
    }

    public function get($id) {
        return $this->db->prepare(
            "SELECT buildinglevel FROM building WHERE buildingid = ?"
        )->execute($id)->fetch();
    }
}

【讨论】:

    【解决方案2】:

    你应该将 mysqli 的实例注入到构建类的 __construct() 中:

    $mysqli = new mysqli('user', 'password', 'localhost', 'test');

    if ($mysqli->connect_errno) { printf("连接失败:%s\n", $mysqli->connect_error); }

    class building
    {
    private $mysql;
    private $buildingid;
    private $userid;
    
    // I need to have a mysqli_query here to get the info for the correct building, 
    //to be able to set the "buildinglevel" for each object from the MYSQL DB, seems basic   
    //but none of the things ive tried has worked.
    
    
    public function __construct($buildingid, $userid, $mysqli)
    {
        $this->buildinglevel;
        $this->mysqli = $mysqli;
        $this->userid = (int)$userid;
        $this->buildingid= (int)$buildingid;
    }
    
    public function getLevel()
    {
        $query = $this->mysqli->query("SELECT ".$this->buildingid." FROM worlds WHERE city_userid=".$this->userid);
        $row = $query->fetch_assoc();
        if (!$query) {
            return $this->mysqli->error;
        }
        if ($query->num_rows == 0) {
            return 'no database records found';
        }
    
        return $row;
    }
    
    }
    
    $Bulding = new building("cityHall", $user['id'], $mysqli);
    $level = $Bulding->getLevel();
    var_dump($level);
    

    【讨论】:

    • 谢谢你的回答,但如果你有时间,你能看看我编辑过的问题吗?我是新来的,我不确定我是否应该这样做,但是,看到那里的一切更清楚,我仍然无法获得空白结果!
    【解决方案3】:

    你的类似乎是所谓的模型:它代表某种形式的数据,在你的例子中是一个特定的建筑物。

    一种方法是将 MySQLi 对象作为构造函数对象传递,以及您要查询的建筑物的 ID,并将结果分配给类属性。如下所示:

    <?php
    class Building
    {
        private $db;
    
        protected $id;
        protected $level;
    
        public function __construct(mysqli $db, $id = null)
        {
            $this->db = $db;
    
            if (!is_null($id) && intval($id) > 0) {
                $stmt = $this->db->prepare("SELECT buildingid, buildinglevel FROM building WHERE `id` = ?");
                $stmt->bind_param('i', $id);
                $stmt->execute();
                $stmt->bind_result($this->id, $this->level);
                $stmt->fetch();
            }
        }
    
        public function getId()
        {
            return (int)$this->id;
        }
    
        public function getLevel()
        {
            return (int)$this->level;
        }
    }
    

    然后您可以像这样获取建筑物的属性:

    $building = new Building($mysqli, 1);
    
    printf('Building ID is %d and building level is %d', $building->getId(), $building->getLevel());
    

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 2015-12-01
      • 2015-11-10
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 2023-03-23
      相关资源
      最近更新 更多