From 2fc7a07e62cff942c1aaf2ea050f7163abdac70a Mon Sep 17 00:00:00 2001 From: Marcus Boerger Date: Mon, 29 Dec 2003 22:26:48 +0000 Subject: [PATCH] Use __tostring() in all printing functions. --- ext/standard/formatted_print.c | 20 ++++++++++++++++---- tests/classes/tostring.phpt | 18 +++++++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/ext/standard/formatted_print.c b/ext/standard/formatted_print.c index 4d141afc45..85ed45abeb 100644 --- a/ext/standard/formatted_print.c +++ b/ext/standard/formatted_print.c @@ -622,15 +622,27 @@ php_formatted_print(int ht, int *len, int use_array, int format_offset TSRMLS_DC PRINTF_DEBUG(("sprintf: format character='%c'\n", format[inpos])); /* now we expect to find a type specifier */ switch (format[inpos]) { - case 's': - convert_to_string_ex(args[argnum]); + case 's': { + zval *var, var_copy; + int use_copy; + + zend_make_printable_zval(*args[argnum], &var_copy, &use_copy); + if (use_copy) { + var = &var_copy; + } else { + var = *args[argnum]; + } php_sprintf_appendstring(&result, &outpos, &size, - Z_STRVAL_PP(args[argnum]), + Z_STRVAL_P(var), width, precision, padding, alignment, - Z_STRLEN_PP(args[argnum]), + Z_STRLEN_P(var), 0, expprec); + if (use_copy) { + zval_dtor(&var_copy); + } break; + } case 'd': convert_to_long_ex(args[argnum]); diff --git a/tests/classes/tostring.phpt b/tests/classes/tostring.phpt index 6aae070135..e62399bde7 100644 --- a/tests/classes/tostring.phpt +++ b/tests/classes/tostring.phpt @@ -42,6 +42,13 @@ echo "====test7====\n"; $ar = array(); $ar[$o->__toString()] = "ERROR"; echo $ar[$o]; + +echo "====test8====\n"; +var_dump(trim($o)); +var_dump(trim((string)$o)); + +echo "====test9====\n"; +echo sprintf("%s", $o); ?> ====DONE!==== --EXPECTF-- @@ -51,7 +58,7 @@ test1 Object ) Notice: Object of class test1 could not be converted to string in %stostring.php on line %d -string(6) "Object" +string(12) "Object id #%d" object(test1)#%d (0) { } ====test2==== @@ -80,4 +87,13 @@ Converted test2::__toString() Warning: Illegal offset type in %stostring.php on line %d +====test8==== + +Notice: Object of class test2 could not be converted to string in %stostring.php on line %d +string(6) "Object" +test2::__toString() +string(9) "Converted" +====test9==== +test2::__toString() +Converted ====DONE!==== -- 2.50.1