Jump to content

Exception __toString(), trying to get HTML output


bfarre01

Recommended Posts

I am trying to create some interesting output from a custom Exception but I am missing a step.  The problem comes in where the HTML in my __toString() output string is encoded.  Below is as simple a custom Exception and __toString() as I could come up with.  I just threw it all in junk.php and let it rip.

How can I prevent the HTML from being encoded?
    Or
How can I code the __toString() to generate HTML output?

[code]
<?php
class MyException extends Exception
{
    public function __construct($m = 'unknown', $c = 0) {
        parent::__construct($m, $c);
    }
    public function __toString() {
        return ": <b>Hello</b>";
    }
}

throw new MyException();

?>
[/code]

Output at command line:
[code]
PHP Fatal error:  Uncaught : <b>Hello</b>
  thrown in C:\...\junk.php on line 11
[/code]

Output in Browser:
[code]<b>Fatal error</b>:  Uncaught : &lt;b&gt;Hello&lt;/b&gt;
  thrown in <b>C:\...\junk.php</b> on line <b>11</b><br />[/code]
Desired Browser Output:
[code]<b>Fatal error</b>:  Uncaught : <b>Hello</b>
  thrown in <b>C:\...\junk.php</b> on line <b>11</b><br />[/code]
Kept digging and found a possilbe solution but it may be a little heavy handed.  If there is a better solution out there please share.  I added "ini_set('html_errors', 0);" to __toString().  I lost some of the other formatting but what I wanted to show up does.  As I was planning to put HTML formatted lists the loss of the bold on the 'Fatal Error', the file name, and line number is a small price to pay.  Below is the new __toString();

  [code]public function __toString() {
    ini_set('html_errors', 0);
    return ": <b>Hello</b>";
  }[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.