Req #70703 [Opn->Wfx]: Scalar type hinting is not consistent in weak mode
| From: | requinix@php.net | Date: | Tue, 13 Oct 2015 01:13:28 +0000 |
| Subject: | Req #70703 [Opn->Wfx]: Scalar type hinting is not consistent in weak mode | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-196583@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70703&edit=1
ID: 70703
Updated by: requinix@php.net
Reported by: benjamin dot morel at gmail dot com
Summary: Scalar type hinting is not consistent in weak mode
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
-Package: PHP Language Specification
+Package: Scripting Engine problem
Operating System: Irrelevant
PHP Version: 7.0.0RC4
Block user comment: N
Private report: N
New Comment:
https://wiki.php.net/rfc/scalar_type_hints_v5
Scalar type hinting was discussed at great lengths on the internals list - and I cannot stress
"great lengths" enough. If you feel up to the challenge then peruse the various threads
that have been created on the subject.
Previous Comments:
------------------------------------------------------------------------
[2015-10-13 00:52:55] benjamin dot morel at gmail dot com
Description:
------------
I think there might still be some minor changes to be done to integer type hinting, in weak mode.
First of all, the current behavior is different whether you pass an invalid string, or an invalid
float:
function test(int $a) {}
test("3 dogs"); // PHP Notice
test(3.5); // silent discard of the fraction
Both result in a truncated piece of information, but one yields a PHP notice while the other
doesn't.
----
That being said, IMHO even in weak mode, I think it's a mistake not to throw a TypeError when
the type conversion involves truncation.
This is what I would suggest:
test(3);
test(3.0);
test(3.1); // TypeError (currently: silent discard)
test('3');
test('3 dogs'); // TypeError (currently: PHP notice)
And no changes in strict mode:
declare(strict_types=1);
test(3);
test(3.0); // TypeError
test(3.1); // TypeError
test('3'); // TypeError
test('3 dogs'); // TypeError
Basically, IMHO again, weak mode should allow other types *as long as they can be safely converted,
without truncation*. Anything else just opens the door to weird, difficult to track bugs.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70703&edit=1