Re: Bug #37538 [Opn->Bgs]: toString in sprintf/printf and %d modifier does't work as expected
| From: | Markus Fischer | Date: | Sun, 21 May 2006 10:54:37 +0000 |
| Subject: | Re: Bug #37538 [Opn->Bgs]: toString in sprintf/printf and %d modifier does't work as expected | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-23611@lists.php.net to get a copy of this message | ||
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
[sorry for sending twice, Marcus, gone through wrong list email]
Hi,
I expected this to be bogusfied. The thing is ... if you put a normal
string for a %d it just works. Now that people learn about __toString()
I think it's not clear why %d doesn't work for an object, because also
(string)$obj, as Marcus pointed out, works.
Is it totally off-side to support objects where also integer would be
expected, were usually a string instead of an integer also works?
Either by introducing __toInt() [ok, I think I better not suggest this]
or at least also call __toString() for integers [which may be actually
ever worse idea] or e.g. call __toInt() if available, if not
__toString() and if not available -> standard behaviour.
Or should I better just not have sent this email?
- - Markus
PHP Bug Database wrote:
> ID: 37538
> Updated by: helly@php.net
> Reported By: mfischer@php.net
> -Status: Open
> +Status: Bogus
> Bug Type: Strings related
> -Operating System: Linux
> +Operating System: *
> -PHP Version: 5CVS-2006-05-21 (CVS)
> +PHP Version: *
> New Comment:
>
> Thank you for taking the time to write to us, but this is not
> a bug. Please double-check the documentation available at
> http://www.php.net/manual/ and the instructions on how to
> report
> a bug at http://bugs.php.net/how-to-report.php
>
> %d is an integer conversion "(int)$obj" as apposed to a string
> conversion that is served by __toString().
>
>
> Previous Comments:
> ------------------------------------------------------------------------
>
> [2006-05-21 05:40:36] mfischer@php.net
>
> Description:
> ------------
> The modifier %d doesn't work as one would expect sprintf/printf. When a
> string is returned, it is always replaced %d with '1' instead of the
> evaluated number of the __toString() output.
>
> Consider this:
>
> printf("%s %d", "4", "4");
>
> will output "4 4", however if the parameters are replaced with an
> object whith magic __toString() which returns "4", %d is not replaced
> with 4 but with 1. It always returned 1, no matter what __toString()
> returns.
>
>
>
> Reproduce code:
> ---------------
> class foo {
> function __toString() {
> return "4";
> }
> }
> printf("%s %d\n", new foo, new foo);
> printf("%s %d\n", "4", "4");
>
>
> Expected result:
> ----------------
> 4 4
> 4 4
>
>
> Actual result:
> --------------
> 4 1
> 4 4
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFEcEbt1nS0RcInK9ARAr/iAJ9ImDxBtQuIFiW51h7qqiHBjIrwfwCfbJTF
2t8X5EqMbvqWvt3F3BK6bjo=
=ladn
-----END PGP SIGNATURE-----