php / phpDoc - @return instance of $this class ?
Posted
by
searbe
on Stack Overflow
See other posts from Stack Overflow
or by searbe
Published on 2011-01-16T14:06:23Z
Indexed on
2011/01/16
22:53 UTC
Read the original article
Hit count: 163
How do I mark a method as "returns an instance of the current class" in my phpDoc?
In the following example my IDE (Netbeans) will see that setSomething always returns a foo
object.
But that's not true if I extent the object - it'll return $this
, which in the second example is a bar
object not a foo
object.
class foo {
protected $_value = null;
/**
* Set something
*
* @param string $value the value
* @return foo
*/
public function setSomething($value) {
$this->_value = $value;
return $this;
}
}
$foo = new foo();
$out = $foo->setSomething();
So fine - setSomething
returns a foo
- but in the following example, it returns a bar
..:
class bar extends foo {
public function someOtherMethod(){}
}
$bar = new bar();
$out = $bar->setSomething();
$out->someOtherMethod(); // <-- Here, Netbeans will think $out
// is a foo, so doesn't see this other
// method in $out's code-completion
... it'd be great to solve this as for me, code completion is a massive speed-boost.
Anyone got a clever trick, or even better, a proper way to document this with phpDoc?
© Stack Overflow or respective owner