|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34286 __toString() behavior is inconsistent
Submitted: 2005-08-28 10:59 UTC Modified: 2005-09-27 10:20 UTC
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: alek at zvuk dot net Assigned: helly (profile)
Status: Closed Package: Feature/Change Request
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
 [2005-08-28 10:59 UTC] alek at zvuk dot net
There are three different ways objects are converted to strings and they are inconsistent (depends on what function you are using). I think all object-to-string conversions should be done via __toString() if it is defined, even if the object is in quotes.

Reproduce code:
	class Message {
		function __toString() {
			return "Hello, World!\n";

	$w = new Message;
	echo("Message: " . $w);

Expected result:
Hello, World!
Message: Hello, World!
Hello, World!
Hello, World!

Actual result:
Object id #1
Message: Object id #1
Hello, World!


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-27 10:20 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 05 04:01:29 2024 UTC