Fatal error: cannot access empty property

I have this error and the line was like this:

$stations=$this->$db->query('SELECT * from service_stations'); 

the $ db variable is declared private, and I use it in the __construct function as follows:

 public function __construct() { //after including the config file $host=DB_HOST; $dbname=DB_NAME; $dbuser=DB_USER; $dbpsw=DB_PASSWORD; try{ $pdo_options[PDO::ATTR_ERRMODE]=PDO::ERRMODE_EXCEPTION; $this->db=new PDO('mysql:host='.$host.';dbname='.$dbname, $dbuser, $dbpsw, $pdo_options); } catch(Exception $e) { die('Erreur: '.$e->getMessage()); } } 

thanks in advance:)

+6
source share
2 answers

You may have made a typo:

 $stations=$this->db->query('SELECT * from service_stations'); // ^ // No $ here ----/ 
+39
source

You probably wanted to write $this->db instead of $this->$db . The former gets access to the db property, the latter gets access to the property, this name is stored in the $db variable. And since this variable is not defined, you are accessing an empty property, as indicated by the error message.

+9
source

Source: https://habr.com/ru/post/886579/


All Articles