# __toString() with cast is broken in php5 RC1

- Thread: https://externals.io/message/8880
- Original archive: https://news-web.php.net/php.internals/8880
- Started by David Giffin on 2004-04-01T23:13:28+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #8880 — David Giffin — 2004-04-01T23:13:28+00:00

URL: https://externals.io/message/8880#8880

Hi There,

I just got a chance tp update to PHP5 RC1 and noticed that some things
have changed for the __toString method. It seems that it it only getting
called when the object is printed or echo. Casting the object to a string
returns the Object #. Here is an example:



---- The Script:

<?php

class obj
{
        function __toString()
        {
                return "yo!!\n";
        }
}

$obj = new obj();
print_r($obj);

$test = (string) $obj;

print $test;
print $obj;

?>

----- Produces:

obj Object
(
)
Object id #1yo!!



Thanks,
David

---

## #8881 — Marcus Boerger — 2004-04-01T23:37:13+00:00

URL: https://externals.io/message/8880#8881 · In reply to: #8880

Hello David,

that's correct.      See the NEWS file, 3rd entry:

18 March 2004, PHP 5 Release Candidate 1
- Fixed numerous bugs with the just-in-time auto-global initialization, that
  could cause $_SERVER, $argv/$argc and other variables not to work properly.
  (Zeev)
- Fixed data corruption with constant assignments to object properties. (Zeev)
- Changed __toString() to be called automatically only with print and echo
  statements. (Andi) 

Friday, April 2, 2004, 1:13:28 AM, you wrote:


> Hi There,

> I just got a chance tp update to PHP5 RC1 and noticed that some things
> have changed for the __toString method. It seems that it it only getting
> called when the object is printed or echo. Casting the object to a string
> returns the Object #. Here is an example:



> ---- The Script:

> <?php

> class obj
> {
>         function __toString()
>         {
>                 return "yo!!\n";
>         }
> }

> $obj = new obj();
> print_r($obj);

> $test = (string) $obj;

> print $test;
> print $obj;

?>>

> ----- Produces:

> obj Object
> (
> )
> Object id #1yo!!



> Thanks,
> David




-- 
Best regards,
 Marcus                            mailto:helly@php.net
