dongwang788787 2017-03-01 14:56
浏览 174
已采纳

PhpStorm无法识别类中的PDO方法

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?

  • 写回答

3条回答 默认 最新

  • douxing1850 2017-03-01 14:59
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料