php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70703 Scalar type hinting is not consistent in weak mode
Submitted: 2015-10-13 00:52 UTC Modified: 2015-10-13 01:13 UTC
From: benjamin dot morel at gmail dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 7.0.0RC4 OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
50 + 12 = ?
Subscribe to this entry?

 
 [2015-10-13 00:52 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-13 01:13 UTC] requinix@php.net
-Status: Open +Status: Wont fix -Package: PHP Language Specification +Package: Scripting Engine problem
 [2015-10-13 01:13 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 14:01:30 2024 UTC