简体   繁体   中英

Extending MySQLi giving me error

I am trying to figure out how can I extend mysqli with my classes.

This is my db class;

class db {
    protected $_server  = "localhost";
    protected $_user    = "root";
    protected $_pwd     = "";
    protected $_db      = "test";
    protected $_charset = "utf8";

    function __construct(){
        $this->createConnection();
    }

    private function createConnection(){
        $this->mysqli = new mysqli($this->_server, $this->_user, $this->_pwd, $this->_db);
        $this->mysqli->query("SET NAMES ".$this->_charset);
        $this->mysqli->query("SET CHARACTER SET ".$this->_charset);
        if($this->mysqli->connect_error) {
            //die('Error ('.$this->mysqli->connect_errno.') '.$this->mysqli->connect_error());
            die();
        }

        //$this->mysqli->close();
    }

    public function mysqli_fetch_object($query){
        $query = $this->mysqli->query($query);
        return $query->fetch_object();
    }

}

and I want to extend this db class in this login class:

require_once("dbClass.php");
class login extends db {
    public function __construct(){

    }
    public function check_login($email, $password) {
        $email          = $this->mysqli->real_escape_string($email);
        $validateMail   = filter_var($email, FILTER_VALIDATE_EMAIL);
        $password       = $password;
        $database_check = $this->mysqli_fetch_object("SELECT * FROM accounts WHERE email = '$validateMail'");

    }
}

and I get these:

Notice: Undefined property: login::$mysqli

Fatal error: Call to a member function real_escape_string() on a non-object

如果覆盖__construct ,则原始构造函数将不会运行,也不会创建连接。

There is no point in extending login from db.
Instead of extending you have to use database class as a service in login class.

class login{
    public function __construct($db){
        $this->db = $db;
    }
    public function check_login($email, $password) {
         $stmt = $this->db->prepare("SELECT * FROM accounts WHERE email = ?");
         ...
    }
}

Also note that your way of running queries is wrong. You have to use prepared statements

First, you use $this->mysqli , but there's no property $mysqli in the db class. Declare it as protected , since the extended class will need access to it:

protected $mysqli;

And second, you must call parent's constructor as other people said, otherwise it won't run.

But as a side note, I suggest you don't extend the db class as login . The best approach I found so far, is to have a db access class, instantiated as a singleton for your app. This way any class that needs it will ask for a singleton instance and use it.

The advantage is obvious: all your db access will occur thru a single instance, making your app much more efficient, because everything will be done thru a single database connection. The way you're doing, as soon as you construct a login instance, a new database connection will be established.

If you keep extending the db class to other classes, like users, products, and so on, new db connections will be created, wasting resources on your server.

You should remove the constructor overload, or in your constructor method you should make a call to parent::__construct . Without it, the parent's constructor is not fired.

class login extends db {
    public function __construct(){
        parent::__construct();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM