【发布时间】:2017-02-08 09:45:36
【问题描述】:
我使用PhpStorm 2016.2.1 IDE。我有一个类型化数组(所有成员都属于相同的已知类),我希望 IDE 知道类型,以便它可以帮助我完成自动完成/智能感知。
class MyClass{
/**
* @var array
*/
public $userArray;
public addUser($uid){ $this->$userArray[$uid] = new User($uid); }
public processUser($uid){
$oUser = $this->$userArray[$uid];
//since the PHP array can contain anything, the IDE makes
//no assumption about what data type $oUser is. How to let it
//know that it's of type User?
}
}
我试过了……
/**
* @var User
*/
public $oUser = ...;
还有
/**
* @type User
*/
public $oUser = ...;
到目前为止,我唯一要做的就是使用 getter 函数:
/**
* @return User
*/
function getUser($uid){ return $this->$userArray[$uid]; }
function processUser($uid){
//now the IDE knows the type of $oUser
$oUser = $this->getUser($uid);
}
但是为了获得更好的 IDE 支持而使用不需要的函数调用来减慢脚本似乎是个坏主意。
知道如何让 PhpStorm 知道变量的类型吗?甚至更好:如何告诉它数组将包含在该数组的 PHPDoc 元数据中的哪种类型?
【问题讨论】:
标签: php ide phpstorm intellisense phpdoc