在主题 class 中找不到 PhpStorm 方法
PhpStorm method not found in subject class
我有一个单独的 class 建立 PDO 连接:
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;
}
}
现在,每次我在另一个 class(例如 $compared_text = Core::getInstance()->getPdo()->query($query_text)->fetchColumn();
)中读取或写入数据库时,即使一切正常,PhpStorm 也会说 "Referenced method is not found in subject class" 关于该方法getPdo()
.
我用谷歌搜索并在 Whosebug 上找到了一些建议使用 PhpDocs 的答案,并且如您所见,我在函数 getPdo()
之前添加了注释 /** @method PDO */
,但它仍然会抛出此错误警告。有什么问题?
UPD
如果我写 $compared_text = Core::getInstance()->Core::getPdo()->query($query_text)->fetchColumn();
,警告就会消失。
所以现在我的问题如下:
我可以在调用每个方法之前不显式写入 Core::
就让警告消失吗?如果不是,我将忽略警告(虽然不想禁用它)。
像这样注释return方法的getInstance()
类型
/**
* @return Core
*/
public static function getInstance() {
if (!isset(self::$instance)) {
$object = __CLASS__;
self::$instance = new $object;
}
return self::$instance;
}
IDE 将读取该注释。
我有一个单独的 class 建立 PDO 连接:
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;
}
}
现在,每次我在另一个 class(例如 $compared_text = Core::getInstance()->getPdo()->query($query_text)->fetchColumn();
)中读取或写入数据库时,即使一切正常,PhpStorm 也会说 "Referenced method is not found in subject class" 关于该方法getPdo()
.
我用谷歌搜索并在 Whosebug 上找到了一些建议使用 PhpDocs 的答案,并且如您所见,我在函数 getPdo()
之前添加了注释 /** @method PDO */
,但它仍然会抛出此错误警告。有什么问题?
UPD
如果我写 $compared_text = Core::getInstance()->Core::getPdo()->query($query_text)->fetchColumn();
,警告就会消失。
所以现在我的问题如下:
我可以在调用每个方法之前不显式写入 Core::
就让警告消失吗?如果不是,我将忽略警告(虽然不想禁用它)。
像这样注释return方法的getInstance()
类型
/**
* @return Core
*/
public static function getInstance() {
if (!isset(self::$instance)) {
$object = __CLASS__;
self::$instance = new $object;
}
return self::$instance;
}
IDE 将读取该注释。