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
 [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-2019 The PHP Group
All rights reserved.
Last updated: Fri Dec 13 20:01:23 2019 UTC