|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2021-02-02 12:38 UTC] david dot m dot wakelin at gmail dot com
Description:
------------
When specifying int as a return type declaration, a type of float is allowed to be returned, which gets cast to an int.
All other type coercions preserve the meaning of the returned data, whereas coercing float to int will lose information after the decimal, therefore not returning the true meaning.
This is not a good feature in my opinion and has been the cause of a couple of uncaught bugs for me.
I suggest that a float to int should be non-coercible in return types and should raise a TypeError as standard and not just when using strict_types.
Test script:
---------------
class Test
{
public static function getInt(): int
{
return 5.67;
}
}
Test::getInt(); // 5 but would like to see a TypeError
Expected result:
----------------
getInt returns a 5, but I think it should raise a TypeError
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 18:00:02 2025 UTC |
in other words: a) if you want strict_types hebavior enable strict_types b) if you can't enable it because of code-mess fix your codebase in weak mode your code literally means and that is how it's supposed to be and your code is in fact written public static function getInt(): int { return (int)5.67; }