Framework: @return void in constructors

Created on 6 Apr 2016  路  2Comments  路  Source: laravel/framework

Many of laravel constructors have @return void in their PHPDoc.

Example:
https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Model.php#L273

It's incorrect because constructors return objects of self type.

Documentation:
https://phpdoc.org/docs/latest/references/phpdoc/tags/return.html

It is RECOMMENDED when documenting to use this tag with every function and method. Exceptions to this recommendation are:

constructors, the @return tag MAY be omitted here, in which case @return self is implied.

We should omit @return void in constructors or replace them with @return self.

Most helpful comment

phpdoc.org is incorrect

All 2 comments

phpdoc.org is incorrect

My 2 cents:

@return self does not make sense, the constructor does not return anything.
@return void could make sense, but PHP 7.1 does not think so:

class Foo {
    public function __construct() : void {}
}

Fatal error: Constructor Foo::__construct() cannot declare a return type

For what it's worth, I think that __construct() is the only exception where @return should be omitted.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jackmu95 picture jackmu95  路  3Comments

JamborJan picture JamborJan  路  3Comments

shopblocks picture shopblocks  路  3Comments

Fuzzyma picture Fuzzyma  路  3Comments

CupOfTea696 picture CupOfTea696  路  3Comments