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

From: Date: Tue, 08 May 2018 16:21:56 +0000
Subject: Req #76312 [NEW]: Returning anything in a constructor/destructor should error
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-215156@lists.php.net to get a copy of this message
From: requinix Operating system: PHP version: 7.2.5 Package: Class/Object related Bug Type: Feature/Change Request Bug description:Returning anything in a constructor/destructor should error 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 bug report at https://bugs.php.net/bug.php?id=76312&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=76312&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=76312&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=76312&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=76312&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=76312&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=76312&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=76312&r=needscript Try newer version: https://bugs.php.net/fix.php?id=76312&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=76312&r=support Expected behavior: https://bugs.php.net/fix.php?id=76312&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=76312&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=76312&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=76312&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=76312&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=76312&r=dst IIS Stability: https://bugs.php.net/fix.php?id=76312&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=76312&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=76312&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=76312&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=76312&r=mysqlcfg

« previous php.bugs (#215156) next »