Search code examples
phpphpstormphpdoc

PHPDoc different return type for extended classes


I have created my own DB - Model structure which is similar to Laravel. I have been facing with 2 problems.

I have a Model class which all of my models extend it. For example, my User class extends Model. I want to return that get() method return type of class which is extended.

Is this possible?

Class Model extends DB {
    /**
     * @return AnyClassThatExtended
     */
    function get()
    {
    }
}

Class User extends Model {
    function test() {
        $user->get(); // I want it to return User type of object
    }
}

Solution

  • You should use

    private static $instance;
    
    /**
     * return static
     */
    public function get() {
        if (is_null(self::$instance)) {
            self::$instance = new static();
        }
    
        return self::$instance;
    }
    

    because you are returning current class that you are at (if I understand correctly)

    It's possible that PHPStorm does not recognize it