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
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: 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

Add a Patch

Pull Requests

Add a Pull Request

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: Sat May 18 13:01:32 2024 UTC