0

我将编辑问题以使其更清晰,以便您可以看到我现在得到的内容,并更容易理解问题。

<?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();

这结果是空白的,没有任何反应。

4

3 回答 3

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);
于 2013-10-10T11:06:32.847 回答
1

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

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

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

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();
    }
}
于 2013-10-10T11:26:21.560 回答
0

您的班级似乎是所谓的模型:它代表某种形式的数据,在您的情况下是特定的建筑物。

一种方法是将 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());
于 2013-10-10T11:14:07.013 回答