用于调用对象内对象的方法的语法

时间:2011-11-13 13:59:23

标签: php oop syntax

可能是一个愚蠢的问题,但我的IDE(PHPStorm)和我有点分歧......

class Item_Backpack {
    public function Empty() {
        // dump contents
    }

    public function insertThing($thing) {
        // insert thing into backpack
    }
}

class Student {
    private $_Backpack; // is a class, can contain other objects

    function __construct() {
        $this->_Backpack = new Item_Backpack;
    }

    public function emptyBackpack() {
        $this->_Backpack->Empty(); // IDE says method undefined
                                   // and cannot give method/property hints
                                   // for this object :-3
    }
}

Item_Backpack类的方法public function Empty() ...... 清空背包!

我的语法是否正确?

1 个答案:

答案 0 :(得分:4)

它有问题因为empty()是PHP中的保留函数名 - 你只需要将函数重命名为其他东西,即。 emptyContents()