Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpStorm does not recognize PDO methods from class

Tags:

php

pdo

phpstorm

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?

like image 961
Scott Taylor Avatar asked Mar 01 '17 14:03

Scott Taylor


3 Answers

Use @var before the definition of the property:

/**
 * @var PDO
 */
private $conn;

That way you tell PhpStorm, that the following variable (property) is of type PDO. Note that private here depends on your code, this is here just an example.

Take a look at this to learn more about PHP Documentation Comments in PhpStorm.

like image 112
Yury Fedorov Avatar answered Nov 20 '22 08:11

Yury Fedorov


The correct thing to do here is declare the $conn class property and put your annotation there

eg

myClass {

/**
* @var PDO
*/
private $conn;
like image 6
edmondscommerce Avatar answered Nov 20 '22 08:11

edmondscommerce


have the same problem in my code.

It seem's that's not the only thing you have to add in your source :

<?php
  /**
   * Created by PhpStorm.
   * User: zac
   * Date: 26/09/2017
   * Time: 12:52
   */

  namespace POO;

  use \PDO;  // <--- need by PhpStorm to find Methods of PDO

  class PersonnagesManager
  {
    /**
     * @var PDO   <--- need by PhpStorm to find Methods of PDO
     */
    private $_db;

    public function __construct($db) {
      $this->setDb($db);
    }

    public function add(Personnage $perso) {
      $q = $this->_db->prepare('INSERT INTO personnages(nom) VALUES(:nom)');
      $q->bindValue(':nom', $perso->nom());
      $q->execute();
      ....

So:

use \PDO;

and

/**
* @var PDO
*/

are both needed !

Work fine for me on version 2017.2.4

like image 2
Christophe ROUXEL Avatar answered Nov 20 '22 08:11

Christophe ROUXEL