Repository navigation
RFC: Deprecate lossy conversions to and from bool - #25889
Open
theodorejb wants to merge 3 commits into
Open
theodorejb wants to merge 3 commits into
theodorejb wants to merge 3 commits into
Conversation
theodorejb
requested review from
DanielEScherzer,
Girgias,
LamentXU123,
SakiTakamachi,
adoy,
bukka,
dstogov and
jorgsowa
as code owners
November 29, 2026 04:12
Member
|
I know @iluuu1994 told me the main reason he voted against my related RFC was because of the int -> bool so his input might be more valuable than mine if special casing 0/1 is sufficient. But overall in favour of the proposal (which shouldn't be a surprise) :) |
In coercive typing mode, emit E_DEPRECATED when a parameter, return value, or typed property coerces a value to bool which has no canonical bool representation (an int other than 0 or 1, any float, or a string other than "0" or "1"), or coerces a bool to int, float, or string. This also applies when a union type without bool, such as int|float or int|string, selects another type for a bool. NAN coerced to bool emits the deprecation in addition to the existing "unexpected NAN value was coerced to bool" warning. zend_parse_arg_str_or_long_slow() no longer falls back to string when the deprecation for the int conversion is promoted to an exception. The compiler and optimizer previously evaluated a constant $strict argument to in_array() with zend_is_true(), bypassing the parameter type check. This is now only done for true and false, so other values go through the regular call. As a side effect, in_array($needle, $haystack, 1) now throws a TypeError under strict_types, as with any other bool parameter. RFC: https://wiki.php.net/rfc/deprecate-lossy-bool-conversions
theodorejb
force-pushed
the
deprecate-lossy-bool-conversions
branch
from
November 29, 2026 04:33
a53b483 to
bf065ce
Compare
The lossy bool conversion deprecation surfaced a number of mistakes in tests, which are fixed here instead of recording the deprecation in the expected output: - A misplaced parenthesis passed the result of `substr(PHP_OS, 0, 3) == 'WIN'` to strtoupper() in the poll test helper and two stream tests. This only worked because true and false become "1" and "". - Trampoline __call() methods used as predicates were declared as returning string instead of bool. - Iterator::valid() implementations returned a filename string or an int counter. - Reflection tests called strlen() on a false doc comment before checking for false. - fread(), fgets(), inet_pton(), iconv_mime_decode(), file_get_contents(), tempnam(), and curl_exec() failures returning false were passed on to string parameters. - Arbitrary strings or ints were passed to the bool parameters of ini_get_all(), strstr(), and substr_compare(). - ob_start() and debug_backtrace() were called with a bool for parameters which were bool before PHP 5.4 and 5.3.6, but are now int flags. - array_filter() was called with true instead of ARRAY_FILTER_USE_BOTH. - A SessionHandlerInterface::gc() implementation returned true instead of the number of deleted sessions. - A get hook for an int property returned the bool result of isset(). - false was passed to an int variadic parameter. - The bool returned by touch() was passed to date() as the timestamp. - false was passed to PDOStatement::execute(), which binds every value as a string, so a user-defined SQLite function received an empty string for its bool parameter. It is now bound with PDO::PARAM_BOOL. - run-tests.php printed the current directory as the loaded php.ini when no php.ini was loaded, as false was passed to realpath().
Tests which only exist to pass each type to one parameter of a function (such as the "different data types" variation tests and "invalid params" tests) are removed, as this type juggling is global behavior already covered by the Zend type declaration tests. Other tests which included booleans in their list of int, float, or string inputs, or other scalars in their list of bool inputs, no longer pass them, so they won't need updating again when the deprecation becomes a TypeError. The fscanf() tests which write booleans to a file as test data now convert them to strings explicitly. The deprecations are recorded only in tests of the coercion behavior itself, such as the scalar and union type declaration tests, exit() values, and the NAN coercion tests, and in the regression test for bug #79919, which needs the bool returned by error_log() to reproduce the bug. The phpGH-22112 test now ignores the deprecation in its error handler, so it continues to cover an exception thrown by the NAN warning. The 32-bit and non-Windows variants of affected tests were updated to match, but could not be run locally.
theodorejb
force-pushed
the
deprecate-lossy-bool-conversions
branch
from
November 29, 2026 04:46
bf065ce to
381e820
Compare
Girgias
reviewed
Nov 29, 2026
Girgias
left a comment
Member
There was a problem hiding this comment.
You might need to review zend_may_throw_ex() function for the ZEND_ASSIGN_OBJ opcode and any opcode that assigns to typed properties as they follow the same semantics as function param/return vals.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://wiki.php.net/rfc/deprecate-lossy-bool-conversions