本文介绍了PHPDoc和后期(静态或动态)绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数PHP IDE都依赖phpdoc来获取有关表达式类型的提示.但是,我经常使用这种模式,但似乎没有涉及到它:

Most PHP IDEs rely on phpdoc to get hints about the type of an expression. Yet, I use frequently this pattern, which doesn't seem to be covered:

class Control {
    private $label = '';

    /** @return ??? */
    public static function Make(){ return new static(); }

    /** @return ??? */
    public function WithLabel($value){  $this->label = $value;  return $this;  }

    /** @return void */
    public function Render(){ /* ... */ }
}

class Textbox extends Control {
   private $text = '';

    /** @return ??? */
    public function WithText($text){  $this->width = $text;  return $this;  }
}

现在我可以使用这样的类:

Now I can use the classes like this:

Textbox::Make()           // <-- late static binding, returns Textbox
   ->WithLabel('foo')     // <-- late dynamic binding, returns Textbox
   ->WithText('bar')      // <-- normal binding, returns Textbox
   ->Render();

是否可以用某种方式替换'???',以使输入信息正确无误?

Is there any way to replace the '???'s with something so that the typing information is correct?

推荐答案

/** @return Control */

非静态:

/** @return $this */

但是phpdoc手册中没有记录

but it's not documented in phpdoc manual

这篇关于PHPDoc和后期(静态或动态)绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 04:36