|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33270 Typecasting an object with a __toString() function returns the string 'Object'.
Submitted: 2005-06-08 03:42 UTC Modified: 2005-06-08 17:22 UTC
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: Jesselnz at gmail dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.0.3 OS: All
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.
Block user comment
Status: Assign to:
Bug Type:
From: Jesselnz at gmail dot com
New email:
PHP Version: OS:


 [2005-06-08 03:42 UTC] Jesselnz at gmail dot com
Typecasting an object with a __toString() function returns the string 'Object' and doesn't run __toString().

Reproduce code:
class Foobar
.....public function __toString ()
..........echo '__toString() was run.', "\n";
..........return ('./includes/foobar.php');

$foobar = new Foobar ();
require ((string) $foobar);

Expected result:
__toString() was run.

require ('./includes/foobar.php');

Actual result:
require ('Object');


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-08 15:07 UTC] nick dot telford at gmail dot com
This is not a bug, as stated in the docs ( __toString() will only be automatically called if directly combined with echo/print with NOTHING BEFORE THE OBJECT! (not even a cast).

echo (string) $obj;

would print Object, since __toString() is not invoked.

__toString()'s implementation is going to be changed/removed completely in PHP5.1
 [2005-06-08 17:22 UTC]
what he said
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Dec 04 01:01:23 2020 UTC