Bug #72516 [Opn->Nab]: strict_types doesn't check the type sent to a callable
| From: | cmb@php.net | Date: | Wed, 29 Jun 2016 17:19:16 +0000 |
| Subject: | Bug #72516 [Opn->Nab]: strict_types doesn't check the type sent to a callable | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-201914@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72516&edit=1
ID: 72516
Updated by: cmb@php.net
Reported by: kinncj@php.net
Summary: strict_types doesn't check the type sent to a
callable
-Status: Open
+Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: CentOS Linux 7/x86_64
PHP Version: 7.0Git-2016-06-29 (Git)
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
That is expected behavior. The documentation states[1]:
| Strict typing applies to function calls made from within the
| file with strict typing enabled, not to the functions declared
| within that file.
Internal functions have to be considered to be declared in a file
with strict_types=0, or as the documentation puts it:
| Function calls from within internal functions will not be
| affected by the strict_types declaration.
See also <https://3v4l.org/0Nc7d>.
[1] <http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict>
Previous Comments:
------------------------------------------------------------------------
[2016-06-29 16:58:59] kinncj@php.net
Description:
------------
when declared strict_types=1, the type check for a callable's parameter is being bypassed.
Test script:
---------------
<?php
declare(strict_types=1);
class IntArray
{
public function __construct(array $initial = [])
{
array_walk($initial, function(int $value){}); // bypass strict_types
foreach ($initial as $data) { $this->typeCheck($data); } // type checked as expected
}
private function typeCheck(int $value)
{
}
}
$array = new IntArray(["12"]);
var_dump($array);
Expected result:
----------------
Fatal error: Uncaught TypeError: Argument 1 passed to {closure} must be of the type integer, string
given, called in /var/tmp/x.php on line 8 and defined in /var/tmp/x.php:12
Stack trace:
#0 /var/tmp/x.php(8): {closure('12')}
#1 /var/tmp/x.php(17): IntArray->__construct(Array)
#2 {main}
thrown in /var/tmp/x.php on line 12
Actual result:
--------------
Fatal error: Uncaught TypeError: Argument 1 passed to IntArray::typeCheck() must be of the type
integer, string given, called in /var/tmp/x.php on line 9 and defined in /var/tmp/x.php:12
Stack trace:
#0 /var/tmp/x.php(9): IntArray->typeCheck('12')
#1 /var/tmp/x.php(17): IntArray->__construct(Array)
#2 {main}
thrown in /var/tmp/x.php on line 12
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72516&edit=1