大多数PHP IDE依靠phpdoc来获取有关表达式类型的提示.然而,我经常使用这种模式,似乎没有涵盖:
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; }
}
Run Code Online (Sandbox Code Playgroud)
现在我可以使用这样的类:
Textbox::Make() // <-- late static binding, returns Textbox
->WithLabel('foo') // …Run Code Online (Sandbox Code Playgroud)