php - 我应该使用@return self、this 还是当前类?

标签 php documentation phpdoc doc

我有一个返回当前对象的方法,我该如何记录呢?

/**
 * 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/

相关文章:

javascript - 获取用于 PHP 查询的 JQuery UI Slider 的值

macos - 我在哪里可以系统地学习如何编写Mac OS X设备驱动程序?

java - 你如何记录匿名函数?

php - 如何将 PHP 代码块放入 PHPDoc DocBlock

php - bool 参数是否有 PHPDoc 类型提示?

php - @return 中有许多不同的类。如何避免将它们全部写下来?

php - 如何按数据库中的两列分组?

php - MySQL innoDB 外键从三个表中级联删除

php - wordpress Contact form 7 验证未与相应的输入对齐

c# - 使用带有文档配置的 aspnet-api-versioning 库时出错