Should I use @return self, this or the current class? [closed] Should I use @return self, this or the current class? [closed] php php

Should I use @return self, this or the current class? [closed]


There is a PHP Standards Recommendation (PSR) currently in draft (PSR-5) that proposes @return $this is used in order to indicate that the same instance is returned.

$this, the element to which this type applies is the same exact instance as the current class in the given context. As such this type is a stricter version of static as, in addition, the returned instance must not only be of the same class but also the same instance.

This type is often used as return value for methods implementing the Fluent Interface design pattern.

This notation is currently used by popular IDEs such as PhpStorm and Netbeans.


@return Current_Class_Name will definitely work and is what I prefer.

@return self may work ok with some programs too.

@return this is bad because this is not a typename.


/** * set something * * @return self */public function setSomething(){            // ...    return $this;}

You can use "self" type as @param or @return..

PHPDoc recommends 'self' to refer to self in object..

Source: http://www.phpdoc.org/docs/latest/references/phpdoc/types.html