php.net |  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
Description:
------------
When strval() is passed an object with a __toString() 
method, strval() should return the results of 
__toString().

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-
archive.com/internals@lists.php.net/msg05832.html

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

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

Expected result:
----------------
foo

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

Patches

Pull Requests

History

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