|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #27125 strval() doesn't work for objects with __toString()
Submitted: 2004-02-03 02:42 UTC Modified: 2004-03-14 17:51 UTC
From: adam at trachtenberg dot com Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 5CVS-2004-02-03 (dev) OS: *
Private report: No CVE-ID: None
 [2004-02-03 02:42 UTC] adam at trachtenberg dot com
When strval() is passed an object with a __toString() 
method, strval() should return the results of 

This makes strval() consistent with (string), which the 
PHP manual agree to be the same thing:

"You can convert a value to a string using the (string) 
cast, or the strval() function."

See also internals list discussion: http://www.mail-

Reproduce code:
class foo {
	public function __toString() {
		return "foo\n";

$foo = new foo;
print strval($foo);

Expected result:

Actual result:
PHP Notice:  Object of class foo could not be converted 
to string in /Users/adam/Documents/php/php5/toString.php 
on line 10


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-14 17:51 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: Sun Mar 03 10:01:29 2024 UTC