php.net |  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
Votes:1
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: alek at zvuk dot net
New email:
PHP Version: OS:

 

 [2005-08-28 10:59 UTC] alek at zvuk dot net
Description:
------------
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:
---------------
<?php
	class Message {
		function __toString() {
			return "Hello, World!\n";
		}
	}

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


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

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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-27 10:20 UTC] helly@php.net
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
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jan 15 08:01:29 2025 UTC