Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to undefined method Closure::query()

I have the following Closure

$dbhProvider = function (){
    //Create connection.
    $instance = new \mysqli('localhost', USERNAME, PASSWORD, 'BLOG');
    return $instance;
};

And i have the following implementation

$mapper = new UserMapper($dbhProvider);

The __constructor of UserMapper looks like this

public function __construct($connection){
    $this->connection = $connection;
    $sql = 'SELECT * FROM USERS WHERE ID=' . $this->user->getId();
    $result = $this->connection->query($sql);
}

And when i exexute i have the following error Call to undefined method Closure::query(). How can i do the properly implement so that the $this->connection instance variable holds the mysqli connection?

like image 294
dios231 Avatar asked Nov 20 '25 11:11

dios231


1 Answers

public function __construct($provider) {
    // invoke the closure/provider/factory
    // so that it returns the mysqli instance
    // which then gets assigned to $this->connection
    $this->connection = $provider();
    $sql = 'SELECT * FROM USERS WHERE ID=' ....
}
like image 159
VolkerK Avatar answered Nov 23 '25 00:11

VolkerK