Doc #73717 [Ver]: return-types with NULL not handeled properly
| From: | cmb@php.net | Date: | Mon, 12 Dec 2016 22:58:55 +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-14221@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:
> frankly i quoted the RFC you linked to!
Ah, I see. Indeed, the *example* with the
integer type declaration has been superseded
by the "Scalar Type Declarations" RFC[1], which introduced the int type
declaration. Note that both RFCs had partially been authored at the same time.
> [â¦] and playing around with function typehints shows clearly a incosistence in the
> RFC's and the real implementations anyways
On an, admittedly, quick glance I haven't been able to find further inconsistencies. Could you
please point them out?
[1] <https://wiki.php.net/rfc/scalar_type_hints_v5>
Previous Comments:
------------------------------------------------------------------------
[2016-12-12 12:10:21] spam2 at rhsoft dot net
>> // Int is not a valid type declaration
> But it is, see <https://3v4l.org/Qoic7>
what do you see there? what i said "luckily no longer true"
frankly i quoted the RFC you linked to!
change it to integer and you get
Fatal error: Uncaught TypeError: Return value of answer() must be an instance of integer, integer
returned
which means you must use "int" and not "integer" and playing around with
function typehints shows clearly a incosistence in the RFC's and the real implementations
anyways
------------------------------------------------------------------------
[2016-12-12 11:58:22] cmb@php.net
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>.
------------------------------------------------------------------------
[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
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=73717
--
Edit this bug report at https://bugs.php.net/bug.php?id=73717&edit=1