I'm trying to use PDO and so I'm putting it in a getter in my Database class:
class Database {
// dsn, username and password here
private $mysql;
public function __construct(){
$mysql = new PDO($this->dsn, $this->dbUsername, $this->dbPassword);
$mysql->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$mysql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->mysql = $mysql;
}
public function database(){
return $this->mysql;
}
}
And I'm calling the database method as such:
class Router extends Database {
public function loggedin(){
$query = $this->mysql()->prepare('SELECT * ...');
$query->execute(array(...));
...
}
However I keep getting the following error:
Fatal error: Call to a member function prepare() on a non-object
I've asked people more knowledgeable than myself about this, however I'm still confused as to why this wouldn't work. Any help and explanation (CS student;- I better understand why) is greatly appreciated!
Thanks!