dounouxi1020 2016-04-12 12:51
浏览 46
已采纳

PhpStorm PDOStatement警告

Consider the following class:

class output_Home {
    public $app;
    public $forums;

    function __construct ($app) {
        // main app class containing db, settings, etc
        $this->app = $app;

        // populate class property arrays for use in template
        $this->setForums();
    }

    function setForums () {
        /*
         * select all forum data, dump into $this->forums array
         * fields:
         *      id, name, slug, description, order,
         *      total_threads, total_posts,
         *      last_post_id, last_post_date, last_poster_id, last_poster_username, last_poster_avatar
         */
        $sql = "select f.*,
                p.id as last_post_id, p.date_created as last_post_date,
                u.id as last_poster_id, u.username as last_poster_username, u.avatar as last_poster_avatar,
                (select count(*) from `threads` where `id_forum`=f.id) as total_threads,
                (select count(*) from `posts` where `id_forum`=f.id) as total_posts
                from `forums` as f
                left join `posts` as p on (p.id = (select `id` from `posts` where `id_forum`=f.id order by `date_created` desc limit 1))
                left join `users` as u on (u.id = p.id_user)
                order by f.order asc";
        $stm = $this->app->db->prepare($sql);
        $stm->execute();
        $this->forums = $this->app->sanitizer->action('sanitize', $stm->fetchAll());
    }
}

Here is my object_App class:

class object_App {
    public $db;

    function __construct () {
        // create database "db" connection
        $this->db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
        $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    }
}

Here is how $this->app is passed to the output_Home class:

$app = new object_App();
$home = new output_Home($app);

The $app property contains a property called db, which is a PDO object. Notice the $stm variable in particular here.

My problem is that I am getting warnings from PhpStorm claiming that methods such as $stm->execute() are not found in the class. Well of course they aren't, because they are PDOStatements.

I just want to know if there is a way I can get rid of the warnings properly, without using PHPDocs above every single $stm I make, because I will be making quite a few in several different class methods, and I don't want PHPDocs everywhere in the code.

Any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • dourang6423 2016-04-12 13:14
    关注

    1. Provide proper type hint for $db. In your particular case this may not be 100% necessary, but better have it (it's one time job)

    class object_App {
        /** @var \PDO */
        public $db;
        ...
    

    In general IDE can figure out what $db is because you have $this->db = new PDO(...); and it's located in __construct() (2 important factors).. but it's better be on safer side with proper type hint. if such line would be placed in some ordinary method (e.g. createDBConnection() or something, IDE will not do that costly extra intelligence.

    2. Most importantly -- provide type hint for your $app. The way how you are passing this argument gives no hints for IDE.

    It could be done like this (and IDE should figure out the rest):

    function __construct (object_App $app) {
    

    Or better in a same way as in #1:

    class output_Home {
        /** @var object_App */
        public $app;
    

    or even better -- combine them together:

    class output_Home {
        /** @var object_App */
        public $app;
    
        function __construct (object_App $app) {
        ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程