简体   繁体   中英

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?

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM