另一类的静态数组变量';对象不允许调用第二类的方法


Static array variable of another class' objects does not allow calling methods of the second class

我不明白为什么这不起作用:

class Test
{
    public static $arData=array();
    public static function addMember(Person $member)
    {
        self::$arData[]=$member;
    }
    public static function showAll()
    {
        for($i=0;$i<count(self::$arData);$i++)
        {
            self::$arData[i]->show();
        }
    }
}

我得到的是:Fatal error: Call to a member function show() on a non-objectshow()方法确实存在,它基本上打印出一个人的姓名和位置。
在构造函数中,我不向$arData添加$member,而是执行$member->show((,它可以工作。

所以。。。怎么了?

尝试

self::$arData[$i]->show();

这个怎么样:

foreach (self::$arData as $person) {
    $person->show();
}

错误在for-循环中:

...
public static function showAll()
{
    for($i=0;$i<count(self::$arData);$i++)
    {
        self::$arData[$i]->show();
    }
}
...

在调用show()-方法时,它必须是数组访问运算符中的$i,而不仅仅是i