Doc #80649 [Opn->Csd]: Incorrect type checking behaviour

From: Date: Wed, 20 Jan 2021 10:50:36 +0000
Subject: Doc #80649 [Opn->Csd]: Incorrect type checking behaviour
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-18428@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80649&edit=1

 ID:                 80649
 User updated by:    graham at alt-three dot com
 Reported by:        graham at alt-three dot com
 Summary:            Incorrect type checking behaviour
-Status:             Open
+Status:             Closed
 Type:               Documentation Problem
 Package:            *General Issues
 Operating System:   Linux
 PHP Version:        8.0.1
 Block user comment: N
 Private report:     N

 New Comment:

The RFC supersedes this.


Previous Comments:
------------------------------------------------------------------------
[2021-01-20 10:44:18] 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:40:57] 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 08:29:27] nicolasgrekas@php.net

Check https://wiki.php.net/rfc/deprecate_null_to_scalar_internal_arg

------------------------------------------------------------------------
[2021-01-20 00:21:38] 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



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=80649&edit=1


Thread (2 messages)

« previous php.doc.bugs (#18428) next »