Not sure exactly how to ask this question. But I'm using PhpStorm as my IDE.
I created a class that will handle my database retrieval and manipulation. In it I have this connect method:
private function connect() {
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->db;
$options = [
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
try {
$this->conn = new PDO($dsn, $this->user, $this->pass, $options);
} catch(PDOException $e) {
$this->error = $e->getMessage();
}
}
The method works fine, but when I go to use $this->conn
in other methods PhpStorm does not recognize $this->conn
as a PDO
object.
Using this method:
private function insert() {
$insert = $this->conn->prepare($sql);
$insert->execute();
}
PhpStorm says that it can't find the method prepare
or execute
.
If I pass $this->conn
into the method and use a PHPDoc block on the parameter it works fine:
/**
* @param $conn pdo
*/
private function insert($conn) {
$insert = $conn->prepare($sql);
$insert->execute();
}
I'm curious as to why it won't let me use $this->conn
? While it's not that much more code to pass $this->conn
to the method, just seems a bit redundant.
Any ideas on what I need to do differently?