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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: benjamin dot morel at gmail dot com
New email:
PHP Version: OS:

 

 [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: Sat Apr 20 02:01:29 2024 UTC