Re: [IDEA for RF C] let the "new" operator fail when the __construct( ) function returns a value.
| From: | Marc B. | Date: | Tue, 17 Feb 2026 07:40:28 +0000 |
| Subject: | Re: [IDEA for RF C] let the "new" operator fail when the __construct( ) function returns a value. | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-130081@lists.php.net to get a copy of this message | ||
Hi Mirco,
Am 17. Februar 2026 07:59:43 MEZ schrieb Mirco Babin <mirco.babin@gmail.com>:
>>Hi Mirco, and welcome!
>>
>>I agree that allowing constructors to return a value leads to confusion,
>>and would support deprecating and then forbidding this.
>>
>>However, I agree with others that this should be checked when compiling
>>the constructor, not when running it under the "new" operator. In other
>>words, make any method named "__construct" give >the same error as a
>>function or method marked ": void".
>
>Hello Rowan Tommins,
>
>I disagree with the compiling part. The __construct() function can
>currently not have a return type, so the return type is implicitly
>"mixed". Changing that would be a BC break for those calling the
>__construct() function as a regular function.
>
>As written to Tim Düsterhus: I have prepared a RFC at
>https://gist.github.com/MircoBabin/aaa574297c8d1baa879f19c99ce28e93
I'm with Roman here as well.
Yes, it's a BC break, but as Roman suggested, there would be a deprecation phase before and
forbidden only in PHP 9. This should give enough time to adopt.
Also, you are talking about the constructor only, which is ok, but I believe the destructor needs to
be discussed as well and mentioned in the RFC.
>
>Kind regards,
>Mirco Babin
Regards,
Marc