====== PHP RFC: Allow void return type on constructors/destructors ====== * Date: 2020-06-16 * Author: Benas Seliuginas, * Target version: PHP 8.0 * Status: Superseded by [[rfc:make_ctor_ret_void]] ===== 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 ===== 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 ==== 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''). 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: 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->__construct();''). ==== 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 ==== 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 ''__clone'' return type as ''void''. Some had said, that the constructor "implicitly"/"indirectly" returns a new object. But: ==== 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. 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 ===== 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]]