我有以下闭包
$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=' ....
}
关于php - 调用未定义的方法 Closure::query(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35458502/