rfc:constructor_return_type

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:constructor_return_type [2020/06/15 23:47] – initial RFC moliatarfc:constructor_return_type [2020/06/16 21:14] – deleted RFC moliata
Line 1: Line 1:
-====== PHP RFC: Allow void return type on constructors/destructors ====== 
-  * Date: 2020-06-16 
-  * Author: Benas Seliuginas <benas.molis.iml@gmail.com> 
-  * Target version: PHP 8.0 
-  * Status: Draft 
  
-===== Introduction ===== 
-This RFC proposes to allow specifying void return type on constructors and destructors. In a way, this optional return type is like a trailing comma: some might use it, some might not, but both cases are valid and allowed. 
- 
-Note, the following RFC suggests to allow specifying an **OPTIONAL** ''void'' return type. It's more or less just a cosmetic addition and by no means is mandatory. If you prefer no return type, that is completely legal. 
- 
-===== Proposal ===== 
-==== Explicit declaration ==== 
-Since a fix for bug #79679 [[https://github.com/php/php-src/pull/5678|is being worked on]], soon it will be illegal to return anything from the constructor (even though no return type means ''mixed|void''). 
-<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::__construct(); 
-    } 
-} 
-</code> 
-In order to signal that the function does not return any value, we should be able (but not forced) to explicitly declare the ''void'' return type. As per the Zen of Python, explicit is always better than implicit. 
- 
-Thus, this RFC suggests allowing to do so: 
-<code php> 
-<?php 
-class Test { 
-    public function __construct(): void {} 
-} 
- 
-class Test2 extends Test { 
-    public function __construct(): void { 
-        // We explicitly state that the 
-        // parent constructor does not 
-        // return. Makes more sense now. 
-        $test = parent::__construct(); 
-    } 
-} 
-</code> 
- 
-==== Documented as void ==== 
-In the PHP manual, both constructor and destructor are specified to have ''void'' return type (e. g. [[https://www.php.net/manual/en/language.oop5.decon.php|here]]). 
- 
-==== __clone allows void return type ==== 
-... 
- 
-==== Consistency with other methods ==== 
-... 
- 
- 
-===== Backward Incompatible Changes ===== 
-None. 
- 
-It is allowed to not a specify a return type. Although, since no return type means ''mixed|void'', it is not legal to "narrow" the return type to ''mixed'' (using covariance), only ''void''. 
- 
-===== Vote ===== 
-2/3 majority Yes/No. 
- 
-===== Implementation ===== 
-[[https://github.com/php/php-src/pull/5717|GitHub Pull Request]] 
rfc/constructor_return_type.txt · Last modified: 2020/06/17 10:04 by cmb