Doc #73717 [Ver]: return-types with NULL not handeled properly

From: Date: Mon, 12 Dec 2016 11:58:24 +0000
Subject: Doc #73717 [Ver]: return-types with NULL not handeled properly
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-14219@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73717&edit=1 ID: 73717 Updated by: cmb@php.net Reported by: spam2 at rhsoft dot net Summary: return-types with NULL not handeled properly Status: Verified Type: Documentation Problem Package: Scripting Engine problem PHP Version: 7.0.14 Block user comment: N Private report: N New Comment: It doesn't make sense to discuss the behavior *here*, because changing it would require an RFC anyway, see <https://wiki.php.net/rfc/howto>. > // Int is not a valid type declaration But it is, see <https://3v4l.org/Qoic7>. Previous Comments: ------------------------------------------------------------------------ [2016-12-12 10:36:52] spam2 at rhsoft dot net luckily at least that below is no longer true because in PHP7 *you must* use int and not integer // Int is not a valid type declaration function answer(): int { return 42; } answer(); Catchable fatal error: Return value of answer() must be an instance of int, integer returned in %s on line %d ------------------------------------------------------------------------ [2016-12-12 10:33:01] spam2 at rhsoft dot net well, and both, not allow NULL in return types as well as for params makes the whole typing for int practically unuseable - the same way as float allows int because it's a subset in non-strict-mode NULL should be casted to 0 (int) or false (bool) because otherwise you have to still use (int)$foo and (bool)$foo in your whole code and instead save overhead because the implicit casting you add overhead by the enforced casting of return values in userland code ------------------------------------------------------------------------ [2016-12-11 23:52:12] cmb@php.net The "Return Type Declarations" RFC explains the reasoning in the section "Disallowing NULL on Return Types"[1]. Apparantly, this info is missing in the manual proper. [1] https://wiki.php.net/rfc/return_types#disallowing_null_on_return_types ------------------------------------------------------------------------ [2016-12-11 21:07:26] spam2 at rhsoft dot net Description: ------------ Fatal error: Uncaught TypeError: Return value of cl_forum::thread_last_timestamp() must be of the type integer, null returned in /mnt/data/www/thelounge.net/contentlounge/cms/modules/forum/api_forum.php:487 why in the world do i need return (int) here in no-strict-types mode the whole purpose of the return type would be to save the manual casting Test script: --------------- public function thread_last_timestamp(int $thread_id=0): int { switch($thread_id) { case 0: return $this->db->fetch_row($this->db->query('select' . SQL_SMALL_CACHE . 'max(fo_post_timestamp) from ' . sql_prefix . 'forum_posts;', 1, 0))[0]; default: return $this->db->fetch_row($this->db->query('select' . SQL_SMALL_CACHE . 'max(fo_post_timestamp) from ' . sql_prefix . 'forum_posts where fo_post_tid=' . $thread_id . ';', 1, 0))[0]; } } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=73717&edit=1

« previous php.doc.bugs (#14219) next »