PhpStorm:如何判断数组成员的 var 类型?
PhpStorm: how to tell the var type of an array member?
我用PhpStorm 2016.2.1
IDE。我有一个类型化数组(所有成员都相同,已知 class),我希望 IDE 知道类型,以便它可以帮助我解决 autocomplete/intellisense。
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
元数据中包含哪种类型?
对于class变量,如你所知,你可以简单地做
@var User
对于局部变量,它不是官方支持的格式,但您还必须指定变量名称:
@var User $oUser
特定于 PHPStorm,您需要使用双星号(我认为这是唯一需要它的 IDE,请考虑):
/** @var User $oUser */
有关详细信息,请参阅 PHPDoc manual
我用PhpStorm 2016.2.1
IDE。我有一个类型化数组(所有成员都相同,已知 class),我希望 IDE 知道类型,以便它可以帮助我解决 autocomplete/intellisense。
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
元数据中包含哪种类型?
对于class变量,如你所知,你可以简单地做
@var User
对于局部变量,它不是官方支持的格式,但您还必须指定变量名称:
@var User $oUser
特定于 PHPStorm,您需要使用双星号(我认为这是唯一需要它的 IDE,请考虑):
/** @var User $oUser */
有关详细信息,请参阅 PHPDoc manual