php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80103 Passing null to new str_* methods in PHP80 doesn't match documentation
Submitted: 2020-09-14 11:50 UTC Modified: 2020-09-14 12:41 UTC
From: prose at zedcore dot com Assigned:
Status: Wont fix Package: Documentation problem
PHP Version: 8.0.0beta3 OS: All
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-09-14 11:50 UTC] prose at zedcore dot com
Description:
------------
The new str_* functions (str_contains / str_starts_with / str_ends_with) act different than described in the documentation with regards to the type hinting. The documentation says that it should be str_starts_with(string $haystack, string $needle), which would suggest that you wouldn't be allowed to pass null. This isn't the case.

This means that the symfony polyfill for PHP80 is unable to polyfill correctly (see https://github.com/symfony/polyfill/issues/282), because either:

 * We widen the typehint to accept null, which means that the polyfill acts differently in comparison to PHP80 in strict mode when null is passed
 * We don't widen the typehint, which means that the polyfill acts different in comparison to PHP80 in weak mode when null is passed

Test script:
---------------
// https://3v4l.org/lSW22V - with strict mode
// https://3v4l.org/PQWAG  - no strict mode


function my_str_starts_with(string $haystack, string $needle): bool
{
    return true;
}

var_dump(str_starts_with(null, 'abc'));
var_dump(my_str_starts_with(null, 'abc'));

Expected result:
----------------
In non-strict mode, the script should error when the str_starts_with provided by PHP80 is run.

Actual result:
--------------
In non-strict mode, null is accepted (and cast to the empty string)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-09-14 11:55 UTC] girgias@php.net
-Status: Open +Status: Wont fix
 [2020-09-14 11:55 UTC] girgias@php.net
This is not limited to the new str_ functions, this applies to all internal functions as ZPP (Zend Parse Parameters) has nullable types per default except in strict_mode.

Changing this would be an engine breaking API change which needs an RFC and documenting each function about this just seems excessive therefore I'm going to close this as WontFix.
 [2020-09-14 12:41 UTC] cmb@php.net
> […], this applies to all internal functions as ZPP (Zend Parse 
> Parameters) has nullable types per default except in strict_mode.

That's not quite correct.  Instead, when passing NULL to an
internal function expecting a string in non-strict type mode, the
NULL is converted to the empty string (per the usual type juggling
rules).

The fact that this is no longer the case for userland functions
should at least be documented.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Nov 29 14:01:24 2020 UTC