开发者

How to set `static/self` return type?

I have main class, and some subclasses, I have static method to return instance

class dad{
    public function __construct($arg){
    }

    // it returns caller class'es object
    public static function get($arg){
        return new static($arg);
    }
}

class son exteneds dad{
    public function __construct($arg){
    }

    public function sonFunc(){

    }
}

class daughter extends dad{
    public function __construct($arg){
    }

    public function daughterFunc(){

    }
}

Now, I want make Ne开发者_开发百科tbeans IDE know, that son::get()-> should be autocomplited by son methods, but daughter::daughter()-> by daughter ones

Is it possible? Or maybe there are any workarounds?

I don't want to override get() method in all subclasses.


Although this is not how I would personally approach either that code layout or the "trick the IDE" hack, a way to make your code work with autocompletion (in Eclipse PDT Helios... maybe NetBeans will act the same) is to (mis)use the @method tag in the class docblocks for your two child classes:

/**
 * @method son get() returns a son
 */
class son extends dad{}
/**
 * @method daughter get() returns a daughter
 */
class daughter extends dad{}

In my IDE testing, this was enough to get autocompletion recognizing that son::get() would return a "son" object, while daughter::get() would return a "daughter" object.

Again, this is a misuse of the @method tag's intent, but it does enable autocompletion in the manner you are asking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜