php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37597 Cannot stringify objects without __toString
Submitted: 2006-05-26 01:49 UTC Modified: 2006-05-27 01:04 UTC
From: doublecompile at gmail dot com Assigned: helly (profile)
Status: Not a bug Package: Strings related
PHP Version: 5.2.* OS: *
Private report: No CVE-ID: None
 [2006-05-26 01:49 UTC] doublecompile at gmail dot com
Description:
------------
Attempting to stringify (casting, strval, echo) an instantiated class without a __toString() method results in the following error:

Catchable fatal error: Object of class test could not be converted to string in...


Built using php5.2-200605252230 Snap with the following config statement:

'./configure' '--with-mysql=/usr/local/php5/ext/mysql' '--with-pdo' '--with-pdo-mysql' '--with-apxs2=/usr/bin/apxs2' '--with-zlib' '--enable-bcmath' '--enable-calendar' '--with-gd=/usr' '--with-jpeg-dir=/usr/include' '--with-zlib-dir=/usr/include' '--with-png-dir=/usr/include' '--with-xpm-dir=/usr/include' '--with-freetype-dir=/usr/include' '--with-ldap' '--with-mcrypt' '--with-mhash' '--with-bz2' '--with-curl' '--enable-ftp' '--with-openssl' '--with-mysqli' '--enable-exif' '--enable-wddx' '--with-xsl' '--enable-sockets' '--enable-soap' '--with-pgsql=/usr/lib/postgresql' '--with-pdo-pgsql=/usr/lib/postgresql' '--with-sqlite' '--with-pdo-sqlite' '--with-ibm-db2=/home/db2inst1/sqllib' '--with-pdo-odbc=ibm-db2,/home/db2inst1/sqllib' '--enable-mbstring' '--with-gettext'

Reproduce code:
---------------
class test { }

$test = new test();

// any of the following would produce the error
$value = (string)$test;
$value = strval($test);
echo $test;


Expected result:
----------------
$value to be set to "Object #1"
or the echo to output "Object #1"

Actual result:
--------------
Catchable fatal error: Object of class test could not be converted to string in...

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-05-26 01:56 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The object id is not useable in any way.
 [2006-05-26 02:03 UTC] doublecompile at gmail dot com
Try this on for size:

<?php

class test {
	public $value;
}

$test = new test();
$test->value = "1234";

$test2 = new test();
$test->value = "5678";

$test3 = new test();
$test->value = "7890";

$example = array( $test, $test2, $test3, $test3 );

$filtered = array_unique( $example );

?>

Outputs:

Catchable fatal error: Object of class test could not be converted to string in ...
 [2006-05-27 01:04 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The array wants to output the contents - so it askes its content to be displayed - however you didn't tell how it should display the objects contents.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 11:01:30 2024 UTC