Bug #80103 [Wfx]: Passing null to new str_* methods in PHP80 doesn't match documentation

From: Date: Mon, 14 Sep 2020 12:41:07 +0000
Subject: Bug #80103 [Wfx]: Passing null to new str_* methods in PHP80 doesn't match documentation
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-17891@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80103&edit=1 ID: 80103 Updated by: cmb@php.net Reported by: prose at zedcore dot com Summary: Passing null to new str_* methods in PHP80 doesn't match documentation Status: Wont fix Type: Bug Package: Documentation problem Operating System: All PHP Version: 8.0.0beta3 Block user comment: N Private report: N New Comment: > […], 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. Previous Comments: ------------------------------------------------------------------------ [2020-09-14 11:55:25] 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 11:50:30] 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) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=80103&edit=1

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