<?PHP
class db{
private $config;
public function __construct(Config $config)
{
$this->config = $config;
$this->connect();
}
private function connect()
{
$host = $this->config->host;
$dbname = $this->$config->base;
$charset = $this->$config->charset;
$user = $this->$config->user;
$pass = $this->$config->pass;
$dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_LAZY,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);
return $this;
}
public function execute($sql)
{
$sth = $pdo->prepare($sql);
return $sth->execute();
}
public function query($sql)
{
$exe = $this->execute($sql);
$res = $exe->fetch(PDO::FETCH_LAZY);
return $res;
}
}
?>
Fatal error: Uncaught TypeError: Argument 1 passed to db::__construct() must be an instance of config, string given, called in
E:\OSPanel\domains\test.ru\index.php on line 40 and defined in
E:\OSPanel\domains\test.ru\classes\_new.db.php:4 Stack trace: #0
E:\OSPanel\domains\test.ru\index.php(40): db->__construct('localhost', 'root', '1111', '1111', 'utf8') #1 {main} thrown in E:\OSPanel\domains\test.ru\classes\_new.db.php on line 4