|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65168 segfault when __toString() returns "$this"
Submitted: 2013-06-30 15:54 UTC Modified: 2013-07-03 19:12 UTC
From: oliver at x10 dot pe Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: master-Git-2013-06-30 (Git) OS: Xubuntu 12.10 64bits
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:
Solve the problem:
31 - 28 = ?
Subscribe to this entry?

 [2013-06-30 15:54 UTC] oliver at x10 dot pe

Casting $this as string inside __toString() magic function makes php crash. It 
seems it ran into an infinite loop. 

Tested on PHP 5.5 and 5.4.11, even 5.3.6

Test script:
class base {
    function __toString() {
        return "$this";
echo new base();

Expected result:
Exception thrown, or an error message.

Actual result:
ViolaciĆ³n de segmento (`core' generado)

[Segmentation Fault ('core' dumped)]


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-30 16:44 UTC]
actually, this is not a "returning" problem,

return "$this" is same as return strval($this), 

so, it's the same as return $this->__toString.

it's a stack overflow segfault
 [2013-06-30 16:46 UTC]
oh, there was a mistake, that is, after we have stackless user function call

return $this should be same as return call_user_func(array($this, "__toString"));
 [2013-06-30 16:53 UTC] oliver at x10 dot pe
laruence, yep I realized I described wrong the bug in the subject after sending 
it >.<
 [2013-07-03 19:12 UTC]
-Status: Open +Status: Not a bug
 [2013-07-03 19:12 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Infinite recursion can lead to stackoverflow and segfault.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 08:01:30 2024 UTC