我有一个返回当前对象的方法,我该如何记录呢?
/**
* set something
*
* @return this
*/
public function setSomething(){
// ...
return $this;
}
或者我应该使用 @return self
还是 @return Current_Class_Name
?
为什么这个问题不是“主要基于意见”(并且应该重新打开)的原因:符合标准和 IDE 类型提示支持。
最佳答案
有一个PHP Standards Recommendation (PSR)目前在草稿中(PSR-5) that proposes @return $this 用于指示返回相同的实例。
$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.
这种表示法目前被流行的 IDE 使用,例如 PhpStorm 和 Netbeans。
关于php - 我应该使用@return self、this 还是当前类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6945564/