Search code examples
phpphp-5.3

Implement finally in PHP 5.5 and below


Is there a way to implement a finally block for PHP versions below 5.5?


Solution

  • You can do like this,

    try {
       # ...
    } catch (Exception $e) {
       # ... but don't re- throw $e!
    }
    
    # ... finally ...
    
    if (isset ($e)) {
        throw $e;
    }
    

    But, I should recommend to upgrade PHP. It shouldn't be that hassle.