Класс БД
<?php
namespace Modules;
use PDO;
class MySqlDatabase
{
protected $_db;
public $last_query;
//Connection to DB
public function __construct(\PDO $pdo)
{
$this->_db = $pdo;
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
//Close connection
public function __destruct()
{
unset($this->_db);
}
public function prepareSelectAll($sql, $class)
{
try {
$stmt = $this->_db->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_CLASS, $class);
return $data;
} catch (\PDOException $e) {
echo "Error prepareALL: " . $e->getMessage() . "<br/>";
echo $this->last_query = $sql;
}
}
Класс User
class User
{
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
private $db;
public function __construct(MySqlDatabase $db){
$this->db = $db;
}
//Select all
public function findAll()
{
$sql = "SELECT * FROM users";
$result_set =$this->db->prepareSelectAll($sql, __CLASS__);
return $result_set;
}
Ошибка вот какая :
Warning: PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: could not call class constructor in C:\openserver\OpenServer\domains\photo_gallery\class\modules\MySqlDatabase.php on line 96
Выяснил что все дело в fetchAll(PDO::FETCH_CLASS, $class); , но не могу понять как исправить это?