php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70915 strict_types = 0 affects only string and bool
Submitted: 2015-11-14 16:51 UTC Modified: 2015-11-14 17:01 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: duodraco@php.net Assigned:
Status: Not a bug Package: *Programming Data Structures
PHP Version: 7.0.0RC7 OS: Linux (4.2.0)
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 !
Your email address:
MUST BE VALID
Solve the problem:
14 + 47 = ?
Subscribe to this entry?

 
 [2015-11-14 16:51 UTC] duodraco@php.net
Description:
------------
Following docs (http://php.net/manual/pt_BR/functions.arguments.php#functions.arguments.type-declaration.strict) when we have no declaration of strict_types or it's set to 0 the default behavior for scalar types is to coerce to declared type.

It's ok for string and boolean. When we set a parameter to int or float PHP throws a TypeError - the expected behavior for strict_types = 1.

When strict_types is set to 1 everything runs as expected.

Test script:
---------------
declare(strict_types = 0);
function test(int $grade)
{
    return $grade >= 7 ? 'X' : 'Y';
}

willInternTurnEmployee(8); //'Pass' and OK

echo willInternTurnEmployee('Really?');
//Expected: 'Fail', string converted to int
//TypeError thrown


Expected result:
----------------
Y

Actual result:
--------------
Fatal error: Uncaught TypeError: Argument 1 passed to willInternTurnEmployee() must be of the type integer, string given, called in /path/to/Projects/php7-demo/app/x.php on line 12 and defined in /path/to/Projects/php7-demo/app/x.php:4
Stack trace:
#0 /path/to/Projects/php7-demo/app/x.php(12): willInternTurnEmployee('Really?')
#1 {main}
  thrown in /path/to/Projects/php7-demo/app/x.php on line 4


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-14 16:54 UTC] duodraco@php.net
It affects both 'Scalar Type Hints' and 'Return Type Declarations'
 [2015-11-14 17:01 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2015-11-14 17:01 UTC] nikic@php.net
Weak type hints follow the same rules as zpp ("Works the same way as internal functions" basically). They do not allow arbitrary coercions, just a lot of them. Here's a table documenting the precise semantics: https://wiki.php.net/rfc/scalar_type_hints_v5#behaviour_of_weak_type_checks
 [2015-11-14 17:25 UTC] duodraco@php.net
Agreed @nikic. It would be good to open an issue to documentation updating it with this conversion table from RFC
 [2015-11-14 17:30 UTC] duodraco@php.net
Sorry. It was a question. Do you think is reasonable to include that table on docs at first link I sent? If yes I can update it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 20:01:31 2024 UTC