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

时间:2016-02-17 13:47:58

标签: php mysql model-view-controller mysqli closures

我有以下关闭

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

我有以下实施

$mapper = new UserMapper($dbhProvider);

__constructor的{​​{1}}看起来像这样

UserMapper

当我执行时,我有以下错误  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连接?

1 个答案:

答案 0 :(得分:3)

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=' ....
}
相关问题