rfc:constructor_return_type
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:constructor_return_type [2020/06/14 20:56] – updated RFC moliata | rfc:constructor_return_type [2020/06/17 10:04] (current) – mark as superseded cmb | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Allow void return type on constructors/ | ====== PHP RFC: Allow void return type on constructors/ | ||
- | * Date: 2020-06-14 | + | * Date: 2020-06-16 |
* Author: Benas Seliuginas, < | * Author: Benas Seliuginas, < | ||
- | * Status: Draft | ||
* Target version: PHP 8.0 | * Target version: PHP 8.0 | ||
+ | * Status: Superseded by [[rfc: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | RFC proposes to allow using '' | + | This RFC proposes to allow specifying |
+ | |||
+ | Note, the following RFC suggests to allow specifying an **OPTIONAL** '' | ||
===== Proposal ===== | ===== Proposal ===== | ||
+ | The introduction sums up the entire proposal therefore I provided a few arguments and thoughts of mine, as to why we should allow this " | ||
+ | ==== Explicit declaration ==== | ||
+ | Since a fix for bug #79679 [[https:// | ||
+ | <code php> | ||
+ | <?php | ||
+ | class Test { | ||
+ | public function __construct() {} | ||
+ | } | ||
+ | |||
+ | class Test2 extends Test { | ||
+ | public function __construct() { | ||
+ | // WTF? Why isn't this legal? | ||
+ | // No return type means mixed|void | ||
+ | // Right? So this should work? | ||
+ | $test = parent:: | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | In order to signal that the function does not return any value, we should be able (but not forced) to explicitly declare the '' | ||
+ | |||
+ | Thus, this RFC suggests allowing to do so: | ||
+ | <code php> | ||
+ | <?php | ||
+ | class Test { | ||
+ | public function __construct(): | ||
+ | } | ||
+ | |||
+ | class Test2 extends Test { | ||
+ | public function __construct(): | ||
+ | // We explicitly state that the | ||
+ | // parent constructor does not | ||
+ | // return. Makes more sense now. | ||
+ | $test = parent:: | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Moreover, it's important to remember that the constructor is rather a regular function that can be called just like any other method (i.e. '' | ||
+ | |||
==== Documented as void ==== | ==== Documented as void ==== | ||
- | ... | + | In the PHP manual, both constructor and destructor are specified to have '' |
- | ==== Explicit declaration | + | ==== __clone allows void return type ==== |
- | ... | + | Given that the [[https:// |
- | ==== Consistency with other methods ==== | + | <code php> |
- | ... | + | <? |
+ | ... | ||
- | ==== __clone() allows | + | $object = new Test(); |
- | ... | + | // |
+ | // and works similarly to a constructor but | ||
+ | // will indeed allow the void return type. | ||
+ | $object_2 = clone $object; | ||
+ | </ | ||
+ | |||
+ | ==== Consistency with other methods | ||
+ | Basically, all (both regular and magic) methods allow to have an explicit return type. The only exceptions are constructors and destructors. | ||
- | Note: only as part of the " | + | As [[https:// |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
None. | None. | ||
- | To ensure backwards compatibility, | + | It is allowed to not a specify a return type. Although, since no return type means '' |
===== Vote ===== | ===== Vote ===== |
rfc/constructor_return_type.1592168218.txt.gz · Last modified: 2020/06/14 20:56 by moliata