Method from child class not found in abstract class

Hello, everyone. I have an abstract class which contains some fields of another abstract type. I have extended both classes with some implementation, however, PhpStorm doesn't know that and keeps saying that Method xxx doesn't exist in abstract class, which essentially is true. Let me provide some code so it's more clear:

abstract class BaseRoom {
   // some methods
}

class Room extends BaseRoom {
   // new methods in addition to the abstract ones
   public function someMethodFromChildClass() {...}
}

abstract class BaseGame {
   /**
     * @var BaseRoom
     */
     protected $_room;
}

class Game extends BaseGame {
     public function something() {
          $this->_room->someMethodFromChildClass(); // Method '..' does not exist in BaseRoom
     }
}


The only way I could tell PhpStorm what $this->_room is was to overwrite it and put the correct phpdoc, but it doesn't seem right to me.
So is there a way to show PhpStorm that $this->_room belongs to a  particular child class of BaseRoom? Or am I doing something wrong?

Thank you very much!!

2 comments
Comment actions Permalink

Hi there,

You have to re-declare it via @property tag to provide correct type hint:

/**
* @property Room _room
*/
class Game extends BaseGame {

...


Otherwise see if you can declare such child-only methods in parent class (abstract method).

0
Comment actions Permalink

Exactly what I needed. Didn't know about this tag. Thanks a lot!!!

0

Please sign in to leave a comment.