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
rfc:constructor_return_type [2020/06/15 23:47] – initial RFC moliatarfc: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/destructors ====== ====== PHP RFC: Allow void return type on constructors/destructors ======
   * Date: 2020-06-16   * Date: 2020-06-16
-  * Author: Benas Seliuginas <benas.molis.iml@gmail.com>+  * Author: Benas Seliuginas<benas.molis.iml@gmail.com>
   * Target version: PHP 8.0   * Target version: PHP 8.0
-  * Status: Draft+  * Status: Superseded by [[rfc:make_ctor_ret_void]]
  
 ===== Introduction ===== ===== Introduction =====
Line 11: Line 11:
  
 ===== 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 "alternative code style".
 ==== Explicit declaration ==== ==== 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''). 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'').
Line 46: Line 47:
 } }
 </code> </code>
 +
 +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. ''$object-><nowiki>__</nowiki>construct();'').
  
 ==== Documented as void ==== ==== Documented as void ====
Line 51: Line 54:
  
 ==== __clone allows void return type ==== ==== __clone allows void return type ====
-...+Given that the [[https://wiki.php.net/rfc/magic-methods-signature|magic methods' signature validation]] RFC is going to pass, we will be able to declare ''<nowiki>__</nowiki>clone'' return type as ''void''Some had said, that the constructor "implicitly"/"indirectly" returns a new object. But: 
 + 
 +<code php> 
 +​<?php​ 
 +​.​.​.​ 
 + 
 +​$​object​ = ​new​ ​Test​(); 
 +​// Also "indirectly" returns a cloned object 
 +​// and works similarly to a constructor but 
 +// will indeed allow the void return type. 
 +​$​object_2​ = clone ​$​object​; 
 +</code>
  
 ==== Consistency with other methods ==== ==== Consistency with other methods ====
-...+Basically, all (both regular and magic) methods allow to have an explicit return typeThe only exceptions are constructors and destructors.
  
 +As [[https://github.com/php/php-src/pull/5717#issuecomment-644419488|Kalle has pointed out]], it is a finger habbit to type a return type after every method and could see himself write this as a style.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/constructor_return_type.1592264832.txt.gz · Last modified: 2020/06/15 23:47 by moliata