Warning: mysqli_query() expects parameter 1 to be mysqli
This is because $link is not actually an instance of mysqli; it is an instance of Connection.
You cannot return anything from the __construct(), as its purpose is only to build an instance of the Connection class itself, not to be a factory for other classes. The return $this->link does not do what you intended.
You have a couple of options.
You can make your class extend mysqli, wherein you are mostly just hard-coding the credentials and calling the parent::__construct() with them, and that eliminates the need for the __destruct() entirely:
// Merely extends mysqli, hard-coding your credentials
class Connection extends mysqli{
// Your constructor has no params, and internally calls
// the parent constructor
public function __construct(){
parent::__construct("localhost","root","","myDatabase");
}
}
The code you wrote to use this would then work immediately, but there isn't much added value, and I don't think this is really what you're going for.
Better option:
If you want to make a class which stores and manages a mysqli object, you can instantiate it but you cannot return it. You need to create a method to return it.
class Connection{
private $link;
// The constructor instantiates the mysqli
public function __construct(){
$this->link = new mysqli("localhost","root","","myDatabase");
}
// Other methods can operate on or manage the mysqli instance
public function __destruct(){
$this->link->close();
}
// Public accessor method to return the connection
public function getConnection() {
return $this->link;
}
}
Then call the method to retrieve the connection.
$link = new Connection();
$sql = "SELECT * FROM atable";
// Call getConnection() to get the actual link
$query = mysqli_query($link->getConnection(), $sql);
$link->__destruct();