Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPDoc for fluent interface in subclass?

Tags:

php

phpdoc

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 { }
like image 295
gremo Avatar asked Mar 06 '26 17:03

gremo


2 Answers

You can return $this instead of person in your docblock

like image 132
Chris Avatar answered Mar 09 '26 07:03

Chris


you have to overwrite your method tag as comment like this

/**
 * @method Student setName($name)
 */
class Student extends Person { }
like image 30
silly Avatar answered Mar 09 '26 05:03

silly