PHP Mysqli 扩展警告 [英] PHP Mysqli Extension Warning

查看:53
本文介绍了PHP Mysqli 扩展警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,但我无法解释为什么它会输出错误.

I have the following code and I can't explain why it outputs an error.

class User extends mysqli{

    // property declaration
    private $server;
    private $user;
    private $pass;
    private $database;
    private $conn;

    function __construct($server, $user, $pass, $database){

        $mysqli = new mysqli($server, $user, $pass, $database);
        if ($mysqli->connect_errno) {
            echo "Connect failed: ". $mysqli->connect_error;
            exit();
        }

        $this->conn = $mysqli;

        return 0;

    }

    function __destruct(){

        $conn = $this->conn;
        $conn->close();

    }
}

和主要代码

include("global_config.php");

require "User.php";

$u = new User($con['server'],$con['user'],$con['pass'],$con['db']);

$u->query("SELECT * FROM users");

返回此错误

Warning: mysqli::query() [mysqli.query]: Couldn't fetch User in E:\Xampp\htdocs\testphp\testMysql.php on line 9

通常,它应该像这里一样 PHP 继承,使用子变量的父函数

normally, it should behave as here PHP inheritance, parent functions using child variables

但是没有

我已经用尽了我的想象力,想着我可以用于谷歌搜索的大多数可能的关键字

i've already exhausted my imagination thinking about most possible keywords I could use for a google search

有什么想法吗?

推荐答案

你做错的第一件事是你的 User 对象扩展了 mysqli 对象,而它们没有任何类似的东西.继承是IS A"关系.对于要使用 Mysqli 对象的 User 对象,您需要HAS A"关系.这也称为组合.

First thing you are doing wrong is that your User object extends mysqli object and they dont have anything similar. Inheritance is "IS A" relationship. And for your User object to use Mysqli object you need "HAS A" relationship. This is also called composition.

如果您使用继承,则不要使用组合,因为您实际上不需要同时使用两者.在这种情况下,继承是错误的选择.

And if you are using inheritance, that don't use composition since you don't need to use both really. In this case, inheritance is wrong choice.

如果您想通过 Mysqli 授予 User 对象访问数据库的权限,您需要在 User 对象中包含 Mysqli 对象.

If you want to give User object access to a database through Mysqli, you need to have Mysqli object inside your User object.

最好的方法是有两个对象,一个代表Mysqli,另一个代表User.

The best way to achive that is to have two objects, one would represent Mysqli, and the other one would represent User.

您的 User 类可能如下所示:

Your User class can look like this:

class User 
{
    private $user;
    private $pass;
    private $db;

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

你会像这样使用它:

$db = new mysqli($server, $user, $pass, $database);
$user = new User($db);

现在用户可以访问Mysqli对象并可以与数据库进行交互.

Now user has access to Mysqli object and can interact with database.

现在更进一步,您可以创建自己的小型数据库抽象层,该层将同时包含 mysqli 和 mysqli stmt 对象,并且在那里您可以拥有执行 CRUD 的函数.然后你将那个数据库抽象层对象传递给你的 User 对象,这样你会更加强大.

Now to go further, you can make your own little database abstraction layer that would hold both mysqli and mysqli stmt objects, and there you could have functions to do CRUD. Then you would pass that database abstraction layer object into your User object and this way you would be even more powerful.

并且不要使用 exit() 函数,只需使用 return 来停止函数.我确切地知道你现在正在经历什么 :) 阅读这篇文章:http://phpmaster.com/liskov-substitution-principle/并通过示例了解为什么组合比继承更好.

And dont use exit() function, just use return to stop a function. I know exactly what are you going through now :) Read this article: http://phpmaster.com/liskov-substitution-principle/ and go through examples to understand why composition is better than inheritance for this.

祝你好运!

这篇关于PHP Mysqli 扩展警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆