[PHP 8.1] Passing null to non-nullable parameters of built-in functions (#1555)

Closes #1554 

Co-authored-by: Máté Kocsis <kocsismate@woohoolabs.com>
Co-authored-by: Christoph M. Becker <cmbecker69@gmx.de>
This commit is contained in:
Sergey Panteleev 2022-04-28 12:22:42 +03:00 committed by GitHub
parent ed8eead5c4
commit 3e087a5c61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1064,6 +1064,38 @@ $func(); // prints "bar"
be thrown in this case.
</simpara>
</note>
<note>
<para>
Scalar types for built-in functions are nullable by default in coercive mode.
As of PHP 8.1.0, passing &null; to an internal function parameter that is not declared nullable
is discouraged and emits a deprecation notice in coercive mode to align with the behavior of user-defined functions,
where scalar types need to be marked as nullable explicitly.
</para>
<para>
For example, <function>strlen</function> function expects the parameter <literal>$string</literal>
to be a non-nullable &string;.
For historical reasons, PHP allows passing &null; for this parameter in coercive mode, and the parameter is
implicitly cast to <type>string</type>, resulting in a <literal>""</literal> value.
In contrast, a <classname>TypeError</classname> is emitted in strict mode.
</para>
<example>
<programlisting role="php">
<![CDATA[
<?php
var_dump(strlen(null));
// "Deprecated: Passing null to parameter #1 ($string) of type string is deprecated" as of PHP 8.1.0
// int(0)
var_dump(str_contains("foobar", null));
// "Deprecated: Passing null to parameter #2 ($needle) of type string is deprecated" as of PHP 8.1.0
// bool(true)
?>
]]>
</programlisting>
</example>
</note>
<sect2 role="seealso">
&reftitle.seealso;