Bug #75824 [Com]: Inconsistent handling of void/null optional return type hints

From: Date: Tue, 16 Jan 2018 16:08:26 +0000
Subject: Bug #75824 [Com]: Inconsistent handling of void/null optional return type hints
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213565@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75824&edit=1 ID: 75824 Comment by: adam at nostradamus dot nu Reported by: adam at nostradamus dot nu Summary: Inconsistent handling of void/null optional return type hints Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: any PHP Version: 7.1.13 Block user comment: N Private report: N New Comment: > What is this script supposed to show? It does not use return types, and as such imposes no > limitations on returns. That a void response is interpreted as null by php without a return type hint. Isn't that a bit weird? Previous Comments: ------------------------------------------------------------------------ [2018-01-16 16:05:33] nikic@php.net What is this script supposed to show? It does not use return types, and as such imposes no limitations on returns. ------------------------------------------------------------------------ [2018-01-16 15:52:31] adam at nostradamus dot nu Hi Nikic, Accoording to this script, that's not entirely true: https://3v4l.org/hn6F0 ------------------------------------------------------------------------ [2018-01-16 15:43:40] nikic@php.net This behavior is intentional. A non-void return type requires an explicit return value. A void return type conversely prohibits an explicit return value (including "return null"). ------------------------------------------------------------------------ [2018-01-16 15:14:48] adam at nostradamus dot nu Description: ------------ We are running into an issue where a function that does not return any value fails in strict mode when an optional return type hint is applied. Observe the following php scripts: https://3v4l.org/o5ufU https://3v4l.org/5TIB5 (the only difference is the explicit null return in the second one) In my opinion, the first one should succeed as well, specifically since the strict check in the following script succeeds: https://3v4l.org/hn6F0 Expected result: ---------------- NULL int(1) Actual result: -------------- Fatal error: Uncaught TypeError: Return value of Foo::getTest() must be of the type integer or null, none returned in /in/o5ufU:10 Stack trace: #0 /in/o5ufU(15): Foo->getTest() #1 {main} thrown in /in/o5ufU on line 10 Process exited with code 255. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75824&edit=1

« previous php.bugs (#213565) next »