php.net |  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
Votes:1
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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
50 + 47 = ?
Subscribe to this entry?

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

Reproduce code:
---------------
<?php
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');

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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 (www.php.net/oop5) __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] sniper@php.net
what he said
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 21:01:31 2024 UTC