Req #76312 [Opn]: Returning anything in a constructor/destructor should error

From: Date: Tue, 08 May 2018 16:28:45 +0000
Subject: Req #76312 [Opn]: Returning anything in a constructor/destructor should error
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-215157@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76312&edit=1 ID: 76312 Updated by: requinix@php.net Reported by: requinix@php.net Summary: Returning anything in a constructor/destructor should error Status: Open Type: Feature/Change Request Package: Class/Object related PHP Version: 7.2.5 Block user comment: N Private report: N New Comment: Actually E_PARSE wouldn't be right. function(): void { return 123; } That will fatal with an E_COMPILE_ERROR, so if this went into 7.3+ then it should be the same level. If it went for 7.1 and 7.2 too then an E_COMPILE_WARNING would probably be better, but given the choice I would rather wait for 7.3 and have it be fatal. Previous Comments: ------------------------------------------------------------------------ [2018-05-08 16:21:54] requinix@php.net Description: ------------ Given that __construct and __destruct do not have return types, PHP should raise an error if the user attempts to return a value. Currently it is allowed but the values are ignored. Naturally an empty return should still be allowed. Targeting only master/7.3+ is fine. https://3v4l.org/q1guZ Request or bug? Feels to me like 51% feature request and 49% bug report. Backwards compatibility? Some quick searches on searchcode.com found some constructors returning values, such as false (private constructors) or $this. Test script: --------------- <?php class Foo { public function __construct() { return 123; } public function __destruct() { return 456; } } Expected result: ---------------- Either E_PARSEs or E_WARNINGs. Should be the same one for both methods. Possibly E_WARNING or E_NOTICE when __construct returns $this: it's understandable but still technically wrong. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76312&edit=1

« previous php.bugs (#215157) next »