Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP type hinting in PHPDoc in derived classes

Take a look at this code example:

class basetype {
    public function method() {
        return false;
    }
}

class extendtype extends basetype {
    public function methodb() {
        return true;
    }
}

class aa {
    /**
     * @var basetype
     */
    protected $membera;
}

class bb extends aa {
    public function __constructor() {
        $this->membera = new extendtype();
    }

    public function dosomething() {
        $this->membera->methodb();
    }
}

When edited within PHPStorm I get warning that "Method methodb not found in class basetype". I work with preexisting code base and can not alter the base classes. So what can I do in order to remove this warning?

like image 734
Darko Miletic Avatar asked Mar 26 '26 12:03

Darko Miletic


1 Answers

You can override $membera in your class BB and give it a new doc-block with the derived type.

class bb extends aa {
    /**
     * @var extendtype
     */
    protected $membera;

    public function __constructor() {
        $this->membera = new extendtype();
    }

    public function dosomething() {
        $this->membera->methodb();
    }
}
like image 186
Charles Avatar answered Mar 29 '26 00:03

Charles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!