简体   繁体   中英

PhpStorm method not found in subject class

I have a separate class that establishes PDO connection:

class Core {
    public $dbh; // handle of the db connection
    private static $instance;

    private function __construct()  {
        // building data source name from config
        $dsn = 'mysql:host=' . Config::read('db.host') . ';dbname=' . Config::read('db.basename') . ';port=' . Config::read('db.port') .';charset=' . Config::read('db.charset') . ';connect_timeout=15';
        //echo '$dsn is '.$dsn;

        // getting DB user from config
        $user = Config::read('db.user');

        // getting DB password from config
        $password = Config::read('db.password');

        $this->dbh = new PDO($dsn, $user, $password);
        $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->dbh->exec("set names utf8mb4");
    }

    public static function getInstance() {
        if (!isset(self::$instance)) {
            $object = __CLASS__;
            self::$instance = new $object;
        }
        return self::$instance;
    }

    /** @method PDO */
    public function getPdo() {
        $core = $this->getInstance();
        $pdo = $core->dbh;
        return $pdo;
    }
}

Now, each time I'm reading from or writing to the db in another class (eg $compared_text = Core::getInstance()->getPdo()->query($query_text)->fetchColumn(); ), even though everything works, PhpStorm says "Referenced method is not found in subject class" regarding the method getPdo() .

I googled and came across some answers here on Stackoverflow that suggested using PhpDocs, and, as you can see, I added a comment /** @method PDO */ before the function getPdo() , but it still throws this warning. What's the issue?

UPD

The warning goes away if I write $compared_text = Core::getInstance()->Core::getPdo()->query($query_text)->fetchColumn(); .

So now my question is as follows:

Can I make the warning go away without explicitly writing Core:: before calling each method from it? If no, I'm just going to ignore the warning (don't want to disable it though).

Annotate the return type of the getInstance() method like this

/**
 * @return Core
 */
public static function getInstance() {
    if (!isset(self::$instance)) {
        $object = __CLASS__;
        self::$instance = new $object;
    }
    return self::$instance;
}

The IDE will read that annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM