From 44a2d9a81eaf5ec18b54c71b40489c323dbee33c Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Wed, 25 Nov 2026 10:21:56 +0100 Subject: [PATCH 1/2] Fix float default values losing precision in reflection output format_default_value() printed floats using the precision INI setting, while constant expressions go through zend_ast_export(), which uses the shortest round-trip representation. So $a = 0.30000000000000004 printed as 0.3, but $b = FOO + 0.30000000000000004 printed in full. --- NEWS | 4 ++ ext/reflection/php_reflection.c | 4 +- .../tests/default_value_float_precision.phpt | 47 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 ext/reflection/tests/default_value_float_precision.phpt diff --git a/NEWS b/NEWS index aabe78ad2a7c..ea9ce1b61a3f 100644 --- a/NEWS +++ b/NEWS @@ -151,6 +151,10 @@ PHP NEWS . Fixed a heap over-read in the interactive shell prompt when cli.prompt is set to an empty string. (Ilia Alshanetsky) +- Reflection: + . Fixed float default values losing precision in __toString() output. + (Jorg Sowa) + - Session: . Fixed session_start() continuing after a failed create_sid() when session.use_strict_mode rejects the supplied ID. (Ilia Alshanetsky) diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index bbe805a81479..c2deea9084a5 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -710,7 +710,9 @@ static zval *get_default_from_recv(const zend_op_array *op_array, uint32_t offse } static void format_default_value(smart_str *str, const zval *value) { - if (smart_str_append_zval(str, value, SIZE_MAX) == SUCCESS) { + if (Z_TYPE_P(value) == IS_DOUBLE) { + smart_str_append_double(str, Z_DVAL_P(value), /* precision */ -1, /* zero_fraction */ true); + } else if (smart_str_append_zval(str, value, SIZE_MAX) == SUCCESS) { /* Nothing to do. */ } else if (Z_TYPE_P(value) == IS_ARRAY) { bool is_list = zend_array_is_list(Z_ARRVAL_P(value)); diff --git a/ext/reflection/tests/default_value_float_precision.phpt b/ext/reflection/tests/default_value_float_precision.phpt new file mode 100644 index 000000000000..e651a83e8aab --- /dev/null +++ b/ext/reflection/tests/default_value_float_precision.phpt @@ -0,0 +1,47 @@ +--TEST-- +Reflection prints float default values with round-trip precision +--INI-- +precision=14 +--FILE-- +getParameters() as $p) { + echo $p, "\n"; +} +echo new ReflectionProperty(C::class, 'p'); +echo $m->getAttributes()[0]; + +?> +--EXPECT-- +Parameter #0 [ $a = 0.30000000000000004 ] +Parameter #1 [ $b = FOO + 0.30000000000000004 ] +Parameter #2 [ $c = [0.1, 1.0, 1.0E+100] ] +Parameter #3 [ $d = -0.0 ] +Parameter #4 [ $e = INF ] +Property [ public $p = 0.30000000000000004 ] +Attribute [ Attr ] { + - Arguments [1] { + Argument #0 [ 0.30000000000000004 ] + } +} From c1409ccb3d81fa73c2513e07bbb8b21f3a795164 Mon Sep 17 00:00:00 2001 From: Jorg Sowa Date: Wed, 25 Nov 2026 12:24:39 +0100 Subject: [PATCH 2/2] Fix float constants losing precision in reflection output Class constants and global constants were printed using the precision INI setting and without a trailing ".0", unlike default values. --- NEWS | 2 +- ext/reflection/php_reflection.c | 4 ++-- .../tests/default_value_float_precision.phpt | 17 ++++++++++++++++- ext/reflection/tests/gh22683.phpt | 2 +- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index ea9ce1b61a3f..9781419c15a2 100644 --- a/NEWS +++ b/NEWS @@ -152,7 +152,7 @@ PHP NEWS set to an empty string. (Ilia Alshanetsky) - Reflection: - . Fixed float default values losing precision in __toString() output. + . Fixed floats losing precision in __toString() output. (Jorg Sowa) - Session: diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index c2deea9084a5..e27dd2ebe111 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -593,7 +593,7 @@ static void _const_string(smart_str *str, const zend_string *name, const zval *v } else if (Z_TYPE_P(value) == IS_STRING) { smart_str_append(str, Z_STR_P(value)); } else if (Z_TYPE_P(value) == IS_DOUBLE) { - smart_str_append_double(str, Z_DVAL_P(value), (int) EG(precision), false); + smart_str_append_double(str, Z_DVAL_P(value), /* precision */ -1, /* zero_fraction */ true); } else if (Z_TYPE_P(value) == IS_FALSE) { smart_str_append(str, ZSTR_KNOWN(ZEND_STR_FALSE)); } else if (Z_TYPE_P(value) == IS_TRUE) { @@ -633,7 +633,7 @@ static void _class_const_string(smart_str *str, const zend_string *name, zend_cl } else if (Z_TYPE(c->value) == IS_OBJECT) { smart_str_appends(str, "Object"); } else if (Z_TYPE(c->value) == IS_DOUBLE) { - smart_str_append_double(str, Z_DVAL(c->value), (int) EG(precision), false); + smart_str_append_double(str, Z_DVAL(c->value), /* precision */ -1, /* zero_fraction */ true); } else if (Z_TYPE(c->value) == IS_FALSE) { smart_str_append(str, ZSTR_KNOWN(ZEND_STR_FALSE)); } else if (Z_TYPE(c->value) == IS_TRUE) { diff --git a/ext/reflection/tests/default_value_float_precision.phpt b/ext/reflection/tests/default_value_float_precision.phpt index e651a83e8aab..fc936d9569ae 100644 --- a/ext/reflection/tests/default_value_float_precision.phpt +++ b/ext/reflection/tests/default_value_float_precision.phpt @@ -1,11 +1,12 @@ --TEST-- -Reflection prints float default values with round-trip precision +Reflection prints floats with round-trip precision --INI-- precision=14 --FILE-- getParameters() as $p) { } echo new ReflectionProperty(C::class, 'p'); echo $m->getAttributes()[0]; +echo new ReflectionClassConstant(C::class, 'K'); +echo new ReflectionClassConstant(C::class, 'L'); +echo new ReflectionClassConstant(C::class, 'W'); +echo new ReflectionConstant('G'); ?> --EXPECT-- @@ -39,9 +49,14 @@ Parameter #1 [ $b = FOO + 0.30000000000000004 ] Parameter #2 [ $c = [0.1, 1.0, 1.0E+100] ] Parameter #3 [ $d = -0.0 ] Parameter #4 [ $e = INF ] +Parameter #5 [ $f = M_PI ] Property [ public $p = 0.30000000000000004 ] Attribute [ Attr ] { - Arguments [1] { Argument #0 [ 0.30000000000000004 ] } } +Constant [ public float K ] { 0.30000000000000004 } +Constant [ public float L ] { 0.3333333333333333 } +Constant [ public float W ] { 1.0 } +Constant [ float G ] { 0.30000000000000004 } diff --git a/ext/reflection/tests/gh22683.phpt b/ext/reflection/tests/gh22683.phpt index 9dfe8eba25c2..9c6fe7565938 100644 --- a/ext/reflection/tests/gh22683.phpt +++ b/ext/reflection/tests/gh22683.phpt @@ -25,4 +25,4 @@ Constant [ float INF ] { INF } Constant [ public float MY_NAN ] { NAN } Constant [ public float MY_INF ] { INF } Constant [ public float MY_FLOAT ] { 1.5 } -Constant [ public float MY_WHOLE ] { 2 } +Constant [ public float MY_WHOLE ] { 2.0 }