FAQ
When trying to derive from class Exception and access the 'string'
property or call 'parent::__toString' PHP segfaults:

<?php
class MyException extends Exception {
function __toString() {
var_dump($this->string);
}
}

throw new MyException("hmm");
?>

Output:

$ php testException.php
NULL
Segmentation fault

Or this example:

<?php
class MyException extends Exception {
function __toString() {
parent::__toString();
}
}

throw new MyException("hmm");
?>

Output:

$ php testException.php
Segmentation fault


The need for this came up when I saw that the reported exception is
not foramted in the HTML output; it's quite hard next to impossible
to properly read the stack trace. So my goal was to make my own
exception and call nl2br() on the string returned from the class.

Besides the segfaults above, is there a chance we have a nicer HTML
formatting for reported Exceptions (while still strip those HTML
tags from it for logging)?

- Markus

Search Discussions

  • Marcus Boerger at Nov 29, 2003 at 5:09 pm
    Hello Markus,

    this works now.

    regards
    marcus (the other one)

    Saturday, November 29, 2003, 3:39:17 PM, you wrote:
    When trying to derive from class Exception and access the 'string'
    property or call 'parent::__toString' PHP segfaults:
    <?php
    class MyException extends Exception {
    function __toString() {
    var_dump($this->string);
    }
    }
    throw new MyException("hmm"); ?>>
    Output:
    $ php testException.php
    NULL
    Segmentation fault
    Or this example:
    <?php
    class MyException extends Exception {
    function __toString() {
    parent::__toString();
    }
    }
    throw new MyException("hmm"); ?>>
    Output:
    $ php testException.php
    Segmentation fault
    The need for this came up when I saw that the reported exception is
    not foramted in the HTML output; it's quite hard next to impossible
    to properly read the stack trace. So my goal was to make my own
    exception and call nl2br() on the string returned from the class.
    Besides the segfaults above, is there a chance we have a nicer HTML
    formatting for reported Exceptions (while still strip those HTML
    tags from it for logging)?
    - Markus



    --
    Best regards,
    Marcus mailto:helly@php.net
  • Markus Fischer at Nov 30, 2003 at 7:37 am
    Hi Marcus,
    On Sat, Nov 29, 2003 at 06:09:36PM +0100, Marcus Boerger wrote :
    Hello Markus,

    this works now.

    regards
    marcus (the other one)
    Thanks, calling parent::__toString() or accessing $this->string
    works now. I've found just another scenario which causes a segfault.

    If you do not return anything from __toString() or return int or an
    object, php segfaults:

    <?php
    class MyException extends Exception {
    function __toString() {
    # segfault
    #return 1;

    # segfault
    #return new StdClass

    # no segfault
    #return array("foo");

    # segfault (no return statement)
    }
    }

    throw new MyException("hmm");
    ?>

    Output:

    $ php testException.php
    Segmentation fault

    - Markus
  • Markus Fischer at Nov 30, 2003 at 10:19 pm
    Hi,
    On Sat, Nov 29, 2003 at 06:09:36PM +0100, Marcus Boerger wrote :
    this works now.
    Thanks. Sorry for the order of reply of the mails, my spam filer
    catches this one and I only discovered it yet.
    Besides the segfaults above, is there a chance we have a nicer HTML
    formatting for reported Exceptions (while still strip those HTML
    tags from it for logging)?
    Can you advise in this issue too?

    The thing is, the stack trace is alright and looks good, but not in
    HTML since the line breaks are ordinary ones; there's not HTML
    formatting for them, so the line break is not visible in the HTML
    output.

    If I derive my own exception and override __toString() I can call
    nl2br(); but then I've the HTML output in the error_log too.

    Seems like an chicken and egg problem. Another solution is to modify
    error_prepend/append to encapuslate the error output in a pre block.


    Besides this possible, would it be feasable to handle this issue
    right away in PHP for HTML and error log output? Maybe it's not
    possible anyway. Hm.

    - Markus
  • Marcus Boerger at Dec 1, 2003 at 6:57 am
    Hello Markus,

    Sunday, November 30, 2003, 10:59:50 PM, you wrote:
    Hi,
    On Sat, Nov 29, 2003 at 06:09:36PM +0100, Marcus Boerger wrote :
    this works now.
    Thanks. Sorry for the order of reply of the mails, my spam filer
    catches this one and I only discovered it yet.
    Besides the segfaults above, is there a chance we have a nicer HTML
    formatting for reported Exceptions (while still strip those HTML
    tags from it for logging)?
    Can you advise in this issue too?
    The thing is, the stack trace is alright and looks good, but not in
    HTML since the line breaks are ordinary ones; there's not HTML
    formatting for them, so the line break is not visible in the HTML
    output.
    If I derive my own exception and override __toString() I can call
    nl2br(); but then I've the HTML output in the error_log too.
    Seems like an chicken and egg problem. Another solution is to modify
    error_prepend/append to encapuslate the error output in a pre block.
    There you go.

    Besides this possible, would it be feasable to handle this issue
    right away in PHP for HTML and error log output? Maybe it's not
    possible anyway. Hm.
    I don't think this makes sense but maybe we *could* try to establish a
    callback into main to display uncaught exceptions. But this must wait until
    after 5 release i think.


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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedNov 29, '03 at 2:58p
activeDec 1, '03 at 6:57a
posts5
users2
websitephp.net

2 users in discussion

Markus Fischer: 3 posts Marcus Boerger: 2 posts

People

Translate

site design / logo © 2022 Grokbase