rfc:make_ctor_ret_void
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:make_ctor_ret_void [2020/07/03 17:28] – updated RFC moliata | rfc:make_ctor_ret_void [2025/03/16 17:55] (current) – updated contact information moliata | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Make constructors and destructors return void ====== | ====== PHP RFC: Make constructors and destructors return void ====== | ||
* Date: 2020-06-17 | * Date: 2020-06-17 | ||
- | * Author: Benas Seliuginas, < | + | * Author: Benas Seliuginas, < |
* Target version: PHP 8.0 | * Target version: PHP 8.0 | ||
- | * Status: | + | * Status: |
Large credit for this RFC goes to Michael Voříšek who initially reported the bug and created a draft-- patch. | Large credit for this RFC goes to Michael Voříšek who initially reported the bug and created a draft-- patch. | ||
Line 42: | Line 42: | ||
* to deprecate the ability of returning values from constructors and destructors in PHP 8.0. | * to deprecate the ability of returning values from constructors and destructors in PHP 8.0. | ||
* to treat both constructors and destructors that do not have an explicit return type, as if they have a return type of '' | * to treat both constructors and destructors that do not have an explicit return type, as if they have a return type of '' | ||
- | * to allow explicit '' | + | * to allow explicit '' |
A deprecation warning would be generated: | A deprecation warning would be generated: | ||
Line 51: | Line 51: | ||
A fatal error would be generated: | A fatal error would be generated: | ||
* for any constructor or destructor that returns a value in PHP 9.0. | * for any constructor or destructor that returns a value in PHP 9.0. | ||
- | * for any constructor or destructor that has an explicit return type other than '' | + | * for any constructor or destructor that has an explicit return type other than '' |
<code php> | <code php> | ||
Line 66: | Line 66: | ||
class Test2 { | class Test2 { | ||
- | // this is legal | + | // this is legal (secondary vote) |
public function __construct(): | public function __construct(): | ||
Line 74: | Line 74: | ||
</ | </ | ||
- | ===== Backward Incompatible Changes | + | ===== Backwards incompatible changes |
Accepting this RFC results in a small backwards compatibility break in PHP 9.0 since it will no longer be legal to return ('' | Accepting this RFC results in a small backwards compatibility break in PHP 9.0 since it will no longer be legal to return ('' | ||
The position of this RFC is that this BC break is minimal, as returning values from constructors/ | The position of this RFC is that this BC break is minimal, as returning values from constructors/ | ||
- | ===== Unaffected | + | ===== Unaffected |
==== Explicit return type declaration is optional ==== | ==== Explicit return type declaration is optional ==== | ||
Explicitly declaring the return type declaration would be optional. It would still be allowed to not specify a type at all: | Explicitly declaring the return type declaration would be optional. It would still be allowed to not specify a type at all: | ||
Line 110: | Line 110: | ||
</ | </ | ||
- | ===== Vote ===== | + | ===== Why allow void return type on constructors/ |
+ | **Enforcing '' | ||
+ | |||
+ | It's key to understand that constructors and destructors in PHP don't work the same way that they do in other languages. First of all, unlike in other languages, constructors and destructors are rather normal functions in PHP and can be called directly i. e. through '' | ||
+ | |||
+ | Rowan Tommins comment on the internals mailing list should also be taken into consideration: | ||
+ | "//The way I look at it, constructors are mostly declared like a normal | ||
+ | method - they use the keyword " | ||
+ | protected, abstract, and final; and can have a parameter list, with | ||
+ | types and defaults - so the surprising thing is that there is a special | ||
+ | rule < | ||
+ | |||
+ | Another argument that is used against allowing explicit '' | ||
+ | |||
+ | ===== Vote ===== | ||
==== Primary ==== | ==== Primary ==== | ||
- | Make constructors | + | <doodle title=" |
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ==== Secondary ==== | ||
+ | <doodle title=" | ||
+ | | ||
+ | * No | ||
+ | </doodle> | ||
===== Implementation ===== | ===== Implementation ===== | ||
[[https:// | [[https:// |
rfc/make_ctor_ret_void.1593797335.txt.gz · Last modified: 2020/07/03 17:28 by moliata