Well apparently my database class was wrong so I've read through a few things and I've attempted to improve it now I'm getting the error message Undefined variable: dsn
<?php
class Database extends PDO
{
protected static $instance;
private $dsn = 'localhost';
private $username = 'root';
private $password = '';
private $dbname = 'shops';
public function __construct($dsn, $username = NULL, $password = NULL, $options = [])
{
$default_options = [
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
];
$options = array_replace($default_options, $options);
parent::__construct($dsn, $username, $password, $options);
}
public static function instance()
{
if (self::$instance === null)
{
self::$instance = new self;
}
return self::$instance;
}
public function run($sql, $args = [])
{
$stmt = $this->pdo->prepare($sql);
$stmt->execute($args);
return $stmt;
}
}
Originally it was
class Database extends PDO
{
private $host = 'localhost';
private $user = 'root';
private $pass = '';
private $dbname = 'shops';
public function __construct($host = null, $user = null, $pass = null, $opts = null)
{
parent::__construct("mysql:host={$this->host};dbname={$this->dbname}", $this->user, $this->pass, $opts);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}