编程语言
首页 > 编程语言> > php-调用未定义的方法Closure :: query()

php-调用未定义的方法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);
}

当我执行时,出现以下错误
 调用未定义的方法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=' ....
}

标签:closures,mysqli,model-view-controller,mysql,php
来源: https://codeday.me/bug/20191119/2032916.html