【问题标题】:PHP MySQLi Object Declaration & RetrievalPHP MySQLi 对象声明和检索
【发布时间】:2012-06-16 03:45:45
【问题描述】:

我有几个模型类处理 db 层,并且在如何以 OO 方式将 db 连接传递给查询时出现逻辑错误。 Class1 是一个 db_connect,我在 class2 查询方法的构造函数中调用它:

class db {
    public $host = 'localhost';
    public $username = 'root';
    public $password = '';
    public $database = 'molecule';
    public $mysqli = '';

   function __construct() {
        $mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
            if (mysqli_connect_errno()) {
                printf("Database Connection failed: %s\n", mysqli_connect_error());
                exit();
            }
        return $mysqli;
    }   
}

class nodeModel {
    public $node;
    public $node_name;
    public $node_link;
    public $node_comment;
    public $mysqli;

    function __construct() {
        $mysqli = new db;
        return $mysqli;
    }

    public $insert;
    public $insert_id;
    function insertNode($node_name, $node_link, $node_comment) {
            $this->insert = $insert;
            $this->insert = $mysqli->prepare("INSERT INTO node(node_name, name_link, node_comment) VALUES (?, ?, ?)");
            $this->insert->bind_param("sss", $this->node_name, $this->node_link, $this->node_comment);
            if($this->insert->execute()) {
                $this->insert_id = $mysqli->insert_id;
            } 
            return $this->insert_id;
            print_r($this->insert_id);
            $this->insert->close();
        }}

但是我收到 $insert 的未定义变量错误。

我使用以下内容调用该层:

include 'node_model.php';
$dbTest = new nodeModel;
$node_name = 'My Node Title';
$node_link = 'My Node Link';
$node_comment = 'My Node Comment. This one should be longer so I will write more stuff';
$dbTest->insertNode($node_name, $node_link, $node_comment);

我与查询类的构造函数连接的方式似乎有效,但不确定我需要在范围内将哪个变量附加到准备/绑定/执行语句?

而且我知道我应该使用 PDO 或其他 ORMy 类型的工具。我会的,但这更多是关于一些基本的 OO 获取/设置/检索最佳实践......感谢您的帮助。

【问题讨论】:

  • PDO 不是“ORMy 类型工具”。如果您了解模型是什么,我会disagree

标签: php oop mysqli this


【解决方案1】:

您的代码存在一些重大问题...

1. 将您的模型类属性设为私有,并创建 setter 和 getter 方法来设置或检索属性的值。你的方法 insertNode 也是错误的。

class nodeModel {
    private $node;
    private $nodeName;
    private $nodeLink;
    private $nodeComment;
    private $mysqli;

    function __construct() {
        $this->mysqli = new db;
    }

    public function getNodeName() {
        return $this->nodeName;
    }

    public function getNodeLink() {
        return $this->nodeLink;
    }

    public function getNodeComment() {
        return $this->nodeComment;
    }

    public function setNodeName($value) {
        $this->nodeName = $value;
    }

    public function setNodeLink($value) {
        $this->nodeLink = $value;
    }

    public function setNodeComment($value) {
        $this->nodeComment = $value;
    }

    function insertNode() {
        $this->insert = $this->mysqli->prepare("INSERT INTO node(node_name, node_link, node_comment) VALUES (?, ?, ?)");
        $this->insert->bind_param($this->node_name, $this->node_link, $this->node_comment);
        if($this->insert->execute()) {
            $this->insert_id = $this->mysqli->insert_id;
        }
        $this->insert->close();

        print_r($this->insert_id);

        return $this->insert_id;
    }}


}

2. 在类db 中创建连接时将其存储到$this->mysqli,而不仅仅是$mysqli

function __construct() {
    $this->mysqli = new mysqli($this->host, $this->username, $this->password, $this->database);
        if (mysqli_connect_errno()) {
            printf("Database Connection failed: %s\n", mysqli_connect_error());
            exit();
        }
    return $this->mysqli;
}

3. 在您的第三个代码中,您只创建了一个传递给方法 insertNode 的变量,但在此方法中您对传递的变量不做任何事情 - 您希望类属性是设置但他们不是...因此这样做:

include 'node_model.php';
$dbTest = new nodeModel;
$dbTest->setNodeName('My Node Title');
$dbTest->setNodeLink('My Node Link');
$dbTest->setNodeComment('My Node Comment. This one should be longer so I will write more stuff');
$dbTest->insertNode();

希望这会有所帮助...

【讨论】:

  • 感谢您的详尽回答。您可以看到我的困惑在于 OOP 中的 get/set 约定以及在程序中完成操作时它所产生的额外工作。
  • 我觉得有趣的是你必须用函数分配变量,然后它们在范围内/即类对象的一部分,所以插入方法不需要传入它们作为参数。
  • 是的。这是一种方法。更简单的方法是将所有属性公开,然后您不必拥有 getter/setter 方法并直接将值分配为 $node->title = 'My Title'; 然后再次调用 $node->insertNode(); 将其另存为新...
【解决方案2】:

但我收到 $insert 未定义变量错误。

那是因为你的函数的第一行是

$this->insert = $insert;
$this->insert = $mysqli->prepare("INSERT INTO node(node_name, name_link, node_comment) VALUES (?, ?, ?)");

$insert 不在您的参数列表中,因此第一行会导致错误。然后在你的第二行中,你还是覆盖了这个变量。

在 return 语句之后还有两行代码,当然它们永远不会执行。

也许是时候睡一觉了? :)

【讨论】:

  • 问题肯定不是因为方法的第一行insertNode... -1
  • 对不起,这是我能看到的唯一问题。
  • 啊,对不起,你不是 OP。然而,这是他报告的唯一错误。
  • 您确定他的代码完全可以正常运行,并且在修复前两行后会执行 OP 想要执行的操作吗?我不这么认为...如果 SE 只是回答问题,那么任何人都可以来回答问题...但是我们应该提供帮助,解决问题,让 OP 了解问题所在...
  • 我只是觉得你有点苛刻,将新手的回答标记为“无益”——仅仅因为 OP 显然对 PHP 中的变量范围有一些误解,这并不意味着处理问题 A(他报告的一个)当问题的根源相同时“没有帮助”
猜你喜欢
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 2018-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多