php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72516 strict_types doesn't check the type sent to a callable
Submitted: 2016-06-29 16:58 UTC Modified: 2016-06-29 17:19 UTC
From: kinncj@php.net Assigned: cmb (profile)
Status: Not a bug Package: *General Issues
PHP Version: 7.0Git-2016-06-29 (Git) OS: CentOS Linux 7/x86_64
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kinncj@php.net
New email:
PHP Version: OS:

 

 [2016-06-29 16:58 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-29 17:19 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-06-29 17:19 UTC] cmb@php.net
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>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 14:01:30 2024 UTC