I ran into an error which I have trouble explaining, and it's probably due to my lack of understanding of how variables work in PHP. Whenever I run my web-app, I get the error that I am trying to execute a method on a non-object, but the parameter has previously been assigned to an object as far as I know.
class PSQLDB implements IDatabase
{
public $DB;
public function __construct($dbConfig)
{
$connString = $dbConfig['driver'] . ':';
foreach ($dbConfig['properties'] as $key => $value)
{
$connString .= $key . '=' . $value . ';';
}
try
{
$this->DB = new PDO($connString, $dbConfig['username'], $dbConfig['password']);
$this->DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (Exception $ex)
{
error_log($ex->getMessage());
}
}
public function getEvents()
{
// TODO: Implement getEvents() method.
}
public function getRecipes()
{
$query = "SELECT * FROM RECIPES";
$statement = null;
$statement = $this->DB->prepare($query);
That's the code snippet on which the last line says that DB is not an object, but if I'm correct, should it not be an object because we set it to one in the constructor?
I'm a bit lost here, can anyone explain why the last line says that DB is not a variable, even though the method in the constructor executed the setAttribute method without trouble? And, more importantly, how can I make sure that he still refers to the object in the other method?