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?
For static methods in classes that may be extended:
/** @return static */
For final-static methods:
/** @return Control */
For non-static methods:
/** @return $this */
but it's not documented in phpdoc manual
Note that nowadays any Intelij IED (like PhpStorm 2019), does support all three
static
,$this
, andself
(as return type in PhpDoc).