我希望我错过了一些简单的东西。我有一个类,它创建另一个类的数组。当我访问数组的元素时,我的IDE(老实说,我还没有在运行时尝试过,因为我还没有完成编码)不能将元素识别为来自原始类。要么我必须习惯它,因为PHP就是这样工作的,要么我需要知道处理它的正确方法。这可能是PHP IDE (PHPStorm)的问题,但是有些地方感觉不对劲。
。 假设我已经定义了一个类:
class memberClubAssoc {
private $_id;
function __construct() {
// do stuff
}
// other various functions
}
…然后我有另一节课。有问题的函数是dealingWithArray(),其中我循环遍历类数组并访问类实例,然后尝试将它们用作类实例。
class member {
private $_id;
private $_aClubAssociations;
function __construct() {
// do stuff
$this->_aClubAssociations = array();
}
// fill in the array with instances of the memberClubAssoc class
function someFunction() {
$this->_aClubAssociations[0] = new memberClubAssoc(1);
$this->_aClubAssociations[1] = new memberClubAssoc(2);
// ...
}
// here's the function in question
function dealingWithArray() {
foreach ($this->_aClubAssociations as $clubAssoc) {
// how does PHP know that $clubAssoc is actually an instance of
// memberClubAssoc? - PHPStorm certainly does not because when
// I try to access a method on the class PHPStorm says it
// doesn't exist.
}
}
}
使用
function dealingWithArray() {
foreach ($this->_aClubAssociations as $clubAssoc) {
/** @var $clubAssoc memberClubAssoc **/
}
}
现在php风暴知道$clubAssoc
中的数据是来自memberClubAssoc
类型的一个类。如果你处理数组值,你需要告诉PHPStorm返回什么类型的数据,因为PHPStorm不能自己获取这个信息。