调用未定义的方法 Closure::query()


Call to undefined method Closure::query()

我有以下闭包

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

我有以下实现

$mapper = new UserMapper($dbhProvider);

UserMapper__constructor看起来像这样

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

当我执行时,我有以下错误 Call to undefined method Closure::query() .如何正确实现以使$this->connection实例变量保持mysqli连接?

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=' ....
}