PHPDoc 在子类中提供流畅的接口?

gre*_*emo 3 php phpdoc

有什么理由让我的 IDE(实际上是 PHPStorm)理解:

$student->setName('Marco');
Run Code Online (Sandbox Code Playgroud)

是否会返回 , 的实例Student而不setName()在子类中重新定义(仅用于添加 PHPDoc 注释)?

class Person
{
    private $name;

    /**
     * @param string $name
     * @return Person
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }
}

class Student extends Person { }
Run Code Online (Sandbox Code Playgroud)

sil*_*lly 5

你必须像这样覆盖你的方法标签作为注释

/**
 * @method Student setName($name)
 */
class Student extends Person { }
Run Code Online (Sandbox Code Playgroud)


Chr*_*ris 5

您可以返回 $this 而不是文档块中的 person