2

I have a Connection class which looks like:

class Connection{
    private $link;

    public function __construct(){
        $this->link = new mysqli("localhost","root","","myDatabase");
        return $this->link;
    }

    public function __destruct(){
        $this->link->close();
    }

}

And I am trying to do this:

$link = new Connection();
$sql = "SELECT * FROM `events`";
$query = mysqli_query($link,$sql);
//Some stuff heref
$link->__destruct();

Is this not valid? I get the following error:

Warning: mysqli_query() expects parameter 1 to be mysqli

3 Answers 3

3

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();
Sign up to request clarification or add additional context in comments.

Comments

2

You should do something like:

<?php

class Connection{
    private $link;


    public function __construct(){
        $this->link = new mysqli("localhost","root","","myliveca_baikalpik");
    }

    public function connect(){
        return $this->link;
    }


}

$con = new Connection();
$link = $con->connect();


$sql = "SELECT * FROM `events`";
$query = mysqli_query($link,$sql);
//Some stuff heref
$link->close();

Note: The __construct method returns null by default. What you need to return is the MySQLi Object.

1 Comment

The __construct method returns the class itself It is not actually true. __construct returns what is used with return, null by default.
1

Return within __construct is useless because new operator returns always instance of given class.

To create an instance of a class, the new keyword must be used. An object will always be created unless the object has a constructor defined that throws an exception on error.

http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.new

Also, try to avoid of calling magic methods directly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.