Search code examples
phpphpdoc

PHPDoc for fluent interface in subclass?


Is there any why to make my IDE (actually PHPStorm) understand that:

$student->setName('Marco');

Will return an instance of Student, without redefining setName() in the subclass (only for adding PHPDoc comments)?

class Person
{
    private $name;

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

        return $this;
    }
}

class Student extends Person { }

Solution

  • You can return $this instead of person in your docblock