rfc:make_ctor_ret_void
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:make_ctor_ret_void [2020/06/17 21:39] – updated RFC moliata | rfc:make_ctor_ret_void [2020/07/06 12:34] – updated RFC moliata | ||
---|---|---|---|
Line 8: | Line 8: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | At the moment, constructors and destructors can return values. However, these magic methods are supposed to be void (according to the documentation) and should not return a value. This RFC proposes to deprecate this behavior in PHP 8.0 and subsequently in PHP 9.0 enforce constructors and destructors | + | At the moment, constructors and destructors can return values. However, these magic methods are supposed to be void (according to the documentation) and should not return a value. This RFC proposes to deprecate this behavior in PHP 8.0 and subsequently in PHP 9.0 enforce |
- | This would apply both implicitly, where no return type is declared for the constructor/ | + | This would apply both implicitly, where no return type is declared for the constructor/ |
===== Proposal ===== | ===== Proposal ===== | ||
==== Status quo ==== | ==== Status quo ==== | ||
- | Currently, void rules are not enforced for constructors and destructors. Thus, it is allowed to return values from those magic methods: | + | Currently, |
<code php> | <code php> | ||
Line 36: | Line 36: | ||
</ | </ | ||
- | But the [[https:// | + | But the [[https:// |
==== Proposal ==== | ==== Proposal ==== | ||
This RFC proposes: | This RFC proposes: | ||
- | * to deprecate the ability of returning | + | * to deprecate the ability of returning values from constructors and destructors in PHP 8.0. |
- | * to treat both contructors | + | * to treat both constructors |
- | * to allow explicit void return type on constructors and destructors. | + | * to allow explicit |
A deprecation warning would be generated: | A deprecation warning would be generated: | ||
- | * for any constructor or destructor that returns a value (< | + | * for any constructor or destructor that returns a value in PHP 8.0 |
+ | |||
+ | Note: if there is an explicit '' | ||
A fatal error would be generated: | A fatal error would be generated: | ||
- | * for any constructor or destructor that returns a value (> | + | * 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 void. | + | * for any constructor or destructor that has an explicit return type other than '' |
<code php> | <code php> | ||
Line 56: | Line 58: | ||
public function __construct() { | public function __construct() { | ||
// this is illegal | // this is illegal | ||
- | // Fatal error: A void function must not return a value | ||
return 0; | return 0; | ||
} | } | ||
- | // this is illegal | + | // this is also illegal |
- | // Fatal error: Destructor Test:: | + | |
public function __destruct(): | public function __destruct(): | ||
} | } | ||
class Test2 { | class Test2 { | ||
- | // this is legal | + | // this is legal (secondary vote) |
public function __construct(): | public function __construct(): | ||
Line 74: | Line 74: | ||
</ | </ | ||
- | ===== Backward | + | ===== Backward |
- | 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 contructors/ | + | 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 95: | Line 95: | ||
==== Constructors are exempt from inheritance checks ==== | ==== Constructors are exempt from inheritance checks ==== | ||
- | Since constructors are exempt from inheritance checks, it is allowed to widen the type from a child class. For example, if the parent class has explicitly declared the constructor as void, it would still be allowed to widen the type to no return type. In other words, covariance (for return types) does not apply to constructors. | + | Since constructors are exempt from inheritance checks, it is allowed to widen the type from a child class. For example, if the parent class has explicitly declared the constructor as '' |
<code php> | <code php> | ||
Line 111: | Line 111: | ||
===== Vote ===== | ===== Vote ===== | ||
- | 2/3 majority | + | Primary: Make constructors and destructors return void? Yes/no. |
+ | |||
+ | Secondary: Allow void return type on constructors/destructors? | ||
===== Implementation ===== | ===== Implementation ===== | ||
[[https:// | [[https:// |
rfc/make_ctor_ret_void.txt · Last modified: 2020/07/22 15:29 by moliata