Bee*_*ice 4 php ide intellisense phpdoc phpstorm
我使用PhpStorm 2016.2.1IDE。我有一个类型化数组(所有成员都属于相同的已知类),我希望 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?
}
}
Run Code Online (Sandbox Code Playgroud)
我努力了...
/**
* @var User
*/
public $oUser = ...;
Run Code Online (Sandbox Code Playgroud)
并且
/**
* @type User
*/
public $oUser = ...;
Run Code Online (Sandbox Code Playgroud)
到目前为止,我唯一要做的就是使用 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);
}
Run Code Online (Sandbox Code Playgroud)
但是仅仅为了获得更好的 IDE 支持而使用不需要的函数调用来减慢脚本速度似乎是一个坏主意。
知道如何让 PhpStorm 知道变量的类型吗?或者甚至更好:如何告诉它数组将在PHPDoc该数组的元数据中包含哪种类型?