php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #80649 Incorrect type checking behaviour
Submitted: 2021-01-20 00:21 UTC Modified: 2021-01-20 10:50 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: graham at alt-three dot com Assigned:
Status: Closed Package: *General Issues
PHP Version: 8.0.1 OS: Linux
Private report: No CVE-ID: None
 [2021-01-20 00:21 UTC] graham at alt-three dot com
Description:
------------
Many mb_ functions are not throwing type errors when the provided input does not match the type signature, on PHP 8.

Test script:
---------------
<?php

function f(string $x) {}

var_dump((new ReflectionFunction('mb_strpos'))->getParameters()[0]->getType()->getName());
var_dump((new ReflectionFunction('f'))->getParameters()[0]->getType()->getName());

mb_strpos(null, 'a');
f(null);


Expected result:
----------------
string(6) "string"
string(6) "string"

Fatal error: Uncaught TypeError: mb_strpos(): Argument #1 ($haystack) must be of type string


Actual result:
--------------
string(6) "string"
string(6) "string"

Fatal error: Uncaught TypeError: f(): Argument #1 ($x) must be of type string


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-01-20 10:40 UTC] cmb@php.net
Indeed, null is coerced to an empty string when passed to an
internal function, even in strict type mode.  I can't find
respective documentation in the manual.
 [2021-01-20 10:41 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 [2021-01-20 10:44 UTC] rtrtrtrtrt at dfdfdfdf dot dfd
> Indeed, null is coerced to an empty string when passed to 
> an internal function, even in strict type mode

no, not in general as you say

Fatal error: Uncaught TypeError: trim() expects parameter 1 to be string, null given in /mnt/data/downloads/test.php:2
 [2021-01-20 10:50 UTC] graham at alt-three dot com
-Status: Open +Status: Closed
 [2021-01-20 10:50 UTC] graham at alt-three dot com
The RFC supersedes this.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC