Skip to content

Silence PHP 8.5 chr() deprecation in encodeInt8#145

Merged
trowski merged 1 commit into
amphp:3.xfrom
webpatser:fix/php85-chr-deprecation
May 16, 2026
Merged

Silence PHP 8.5 chr() deprecation in encodeInt8#145
trowski merged 1 commit into
amphp:3.xfrom
webpatser:fix/php85-chr-deprecation

Conversation

@webpatser
Copy link
Copy Markdown
Contributor

PHP 8.5 deprecates chr() with values outside [0, 255]. The warning fires from src/MysqlDataType.php:521 because encodeInt8 receives negative ints from MysqlEncodedValue::fromValue() for the Tiny branch (parameters in [-128, 127]).

The fix swaps \chr($int) for \pack("C", $int), which produces the same byte across the full input range and isn't deprecated. It's also consistent with the existing idiom in this file:

encodeInt16 → \pack("v", $int)
encodeInt24  \substr(\pack("V", $int), 0, 3)
encodeInt32  \pack("V", $int)
encodeInt8   \pack("C", $int)  // this PR

Verification

Bit-identical output across the full input range that encodeInt8 actually sees ([-128, 250]):

php -r 'for ($i = -128; $i < 256; $i++) { assert(chr($i & 0xff) === pack("C", $i)); }'   // silent pass

A direct smoke test of the swapped function:

encodeInt8(-128) = 0x80
encodeInt8(  -1) = 0xff
encodeInt8(   0) = 0x00
encodeInt8( 127) = 0x7f
encodeInt8( 200) = 0xc8
encodeInt8( 255) = 0xff

No deprecation triggered after the change.

The other \chr() call sites in the codebase (src/Internal/ConnectionProcessor.php) all pass small non-negative values (cursor flags, bind counts, charset constants, length bytes) and don't trigger the deprecation, so they're left alone.

…tion

PHP 8.5 deprecates chr() with values outside [0, 255] (modulo-256 silent
fallback). encodeInt8 is called from MysqlEncodedValue::fromValue for int
parameters in [-128, 127] (the Tiny branch), and from encodeInt for length
prefixes up to 250, so it routinely receives negative ints that trigger
the warning.

pack("C", $int) produces the same byte across the full input range and
matches the existing pack-family idiom used by encodeInt16/24/32:

    encodeInt16 → \pack("v", $int)
    encodeInt24 → \substr(\pack("V", $int), 0, 3)
    encodeInt32 → \pack("V", $int)
    encodeInt8  → \pack("C", $int)  (this commit)

Verified bit-identical to the old chr() output for the full [-128, 255]
input range, no behavior change.
@trowski
Copy link
Copy Markdown
Member

trowski commented May 16, 2026

Thanks!

@trowski trowski merged commit ea3a5e0 into amphp:3.x May 16, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants