Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a method that return the current object, how do I document this?

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

Or should I do @return self or @return Current_Class_Name?


Reason why this question is not "primarily opinion-based" (and should be reopened): conformance to standards and IDE type hinting support.

like image 553
lucaswxp Avatar asked Aug 04 '11 17:08

lucaswxp


People also ask

Do I need to return self python?

Need for Self in Python Python uses the self parameter to refer to instance attributes and methods of the class. Unlike other programming languages, Python does not use the “@” syntax to access the instance attributes. This is the sole reason why you need to use the self variable in Python.

What does return self mean in Python?

Returning self from a method simply means that your method returns a reference to the instance object on which it was called. This can sometimes be seen in use with object oriented APIs that are designed as a fluent interface that encourages method cascading.


1 Answers

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.

like image 89
g . Avatar answered Oct 05 '22 07:10

g .