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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度