PHP equivalent to eval { die(); } ; if ($@) { }

    Date: 04/27/07 (PHP Community)    Keywords: php, xml

    Is there a nice way in PHP can one can run a bit of code (in this case, a call to Smarty to render an XML document) and basically catch it if throws an error. My understanding is that eval in PHP only turns strings into executable code but doesn't catch dies/exits in the same way as Perl.

    The only way that I can think of doing it is by using a less than ellegant error handler hack:

    set_error_handler('empty_handler', E_ALL);
    $output = $smarty->fetch('xml.tmpl');
    restore_error_handler();
    
    if (isset($GLOBALS['errno']) && !empty($GLOBALS['errno'])) {
      // Do some backing out stuff here
    
      // Now pass over to the normal (and restored) error handler
      trigger_error($GLOBALS['errmsg'], $GLOBALS['errno']);
    }
    
    function empty_handler($errno, $errmsg, $filename, $linenum, $vars) {
      // Look out, nasty globals being used to feed back the data
      $GLOBALS['errno'] = $errno;
      $GLOBALS['errmsg'] = $errmsg;
    }
    

    There's got to be a nicer way to do this, right?

    P.S. I'm using Smarty as an example of any third party code that you might want to run that either has a trigger_error() burried in it that you might not have control over when calling.

    Source: http://community.livejournal.com/php/562933.html

« USPS Web Tools API? || text areas »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home