]> granicus.if.org Git - php/commitdiff
Use __tostring() in all printing functions.
authorMarcus Boerger <helly@php.net>
Mon, 29 Dec 2003 22:26:48 +0000 (22:26 +0000)
committerMarcus Boerger <helly@php.net>
Mon, 29 Dec 2003 22:26:48 +0000 (22:26 +0000)
ext/standard/formatted_print.c
tests/classes/tostring.phpt

index 4d141afc457c348c616b349aaf155bfc29589814..85ed45abeb590cee9c02cf2ef0772d090a266236 100644 (file)
@@ -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]);
index 6aae0701354dd4a22063a7d2b570ae84173690c8..e62399bde78ab37edd4bcd6079e006741a6af0ae 100644 (file)
@@ -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!====