[PHP-BUG] Req #70703 [NEW]: Scalar type hinting is not consistent in weak mode
[email protected] ("benjamin dot morel at gmail dot com")
| Newsgroups | php.standards |
|---|---|
| Message-ID | <[email protected]> |
From: benjamin dot morel at gmail dot com
Operating system: Irrelevant
PHP version: 7.0.0RC4
Package: PHP Language Specification
Bug Type: Feature/Change Request
Bug description:Scalar type hinting is not consistent in weak mode
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 bug report at https://bugs.php.net/bug.php?id=70703&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70703&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70703&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70703&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=70703&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=70703&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=70703&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=70703&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=70703&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=70703&r=support
Expected behavior: https://bugs.php.net/fix.php?id=70703&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=70703&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=70703&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=70703&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70703&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=70703&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=70703&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=70703&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70703&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=70703&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=70703&r=mysqlcfg