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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Mon Feb 26 08:01:27 2024 UTC