Drew
Drew

Reputation: 6274

syntax for calling method of an object inside an object

Probably a stupid question, but my IDE (PHPStorm) and I are having a bit of a disagreement...

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
    }
}

The Item_Backpack class has the method public function Empty() which ... empties the backpack!

Is my syntax correct here?

Upvotes: 3

Views: 792

Answers (1)

Chris
Chris

Reputation: 684

It's having problems because empty() is a reserved function name in PHP - you just need to rename the function to something else, ie. emptyContents()

Upvotes: 4

Related Questions