title: How to handle PHP 7 Error exceptions using framework exception handling in ZF2?
body: <p>For (Zend Framework) exceptions I use a simple mechanism shown below:</p>
  
  <pre><code>namespace Application;
  
  use Zend\Mvc\ModuleRouteListener;
  use Zend\Mvc\MvcEvent;
  use Zend\Log\Logger;
  
  class Module
  {
      public function onBootstrap(MvcEvent $e)
      {
          $eventManager = $e-&gt;getApplication()-&gt;getEventManager();
          $moduleRouteListener = new ModuleRouteListener();
          $moduleRouteListener-&gt;attach($eventManager);
          // ...
          $this-&gt;initExceptionHandler($e);
      }
  
      public function initExceptionHandler($event)
      {
          $sharedManager = $event-&gt;getApplication()-&gt;getEventManager()-&gt;getSharedManager();
          $serviceManager = $event-&gt;getApplication()-&gt;getServiceManager();
          $sharedManager-&gt;attach(
              'Zend\Mvc\Application',
              [MvcEvent::EVENT_DISPATCH_ERROR, MvcEvent::EVENT_RENDER_ERROR], 
              function ($event) use($serviceManager) {
                  // error log
                  // ...
                  // error page
                  ...
              }
          );
      }
  }
  </code></pre>
  
  <p>Now I want to implement the error handling for the native PHP errors. In PHP 7 "<em>most errors are now reported by throwing <a href="http://php.net/manual/en/class.error.php" rel="nofollow">Error</a> exceptions</em>" (s. <a href="http://php.net/manual/en/language.errors.php7.php" rel="nofollow">here</a>). So they can be easily caught: either with a <code>try-catch</code> block or via the <a href="http://php.net/manual/en/function.set-exception-handler.php" rel="nofollow"><code>set_exception_handler(...)</code></a>.</p>
  
  <p>In order to avoid needless code duplication, I want to "convert" PHP error exceptions to exceptions catchable by the already implemented mechanism. The idea is to "wrap" errors in <code>MvcEvent</code>s and forward them to the (framework) exception error handling. What I've tried so far:</p>
  
  <pre><code>// ...
  class Module
  {
      public function onBootstrap(MvcEvent $e)
      {
          // ...
          $this-&gt;initExceptionHandler($e);
          $this-&gt;initErrorHandler($e);
      }
      // ...
      public function initErrorHandler($event)
      {
          set_exception_handler(function (\Throwable $exception) use ($event) {
              $event-&gt;getApplication()-&gt;getEventManager()-&gt;trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event);
          });
      }
  }
  </code></pre>
  
  <p>The result is only a white page and no error messages (so nothing on the page, nothing new in the custom error log, nothing new in the <code>syslog</code>).</p>
  
  <p><strong>How to dock/connect the PHP native error handling to the (framework) exception handling, so that errors cause triggering of error events and can be processed as (framework) exceptions?</strong></p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The problem with the logging was, that the <code>$event#params['exception']</code> was not set, so the line <code>$serviceManager-&gt;get('ErrorLogger')-&gt;crit($event-&gt;getParam('exception'), $extra);</code> in the <code>Module#initExceptionHandler(...)</code> could not be executed. Now I'm setting this and some other parameters of the <code>$event</code> manually:</p>
  
  <pre><code>public function initErrorHandler(MvcEvent $event)
  {
      set_exception_handler(function (\Throwable $exception) use ($event) {
          $event-&gt;setError(Application::ERROR_EXCEPTION);
          $routeMatch        = $event-&gt;getRouteMatch();
          $controllerName    = 'Application\Controller\Error';
          $event-&gt;setController($controllerName);
          $controllerManager = $event-&gt;getApplication()-&gt;getServiceManager()-&gt;get('controller_manager'); // -&gt;get('controllers');
          $controller = $controllerManager-&gt;get($controllerName);
          $event-&gt;setControllerClass(get_class($controller));
          $event-&gt;setParam('exception', $exception);
          $viewModel = new ViewModel();
          $viewModel-&gt;setCaptureTo('content');
          $viewModel-&gt;setTemplate('error/index');
          $viewModel-&gt;setTerminal(false);
          $viewModel-&gt;setVariables([
              'message' =&gt; 'An error occurred during execution; please try again later.',
              'display_exceptions' =&gt; true
          ]);
          $event-&gt;setResult($viewModel);
          // From this moment the framework exception handling starts and cares about the logging and displaying the error data.
          // $event-&gt;getApplication()-&gt;getEventManager()-&gt;trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event);
          $event-&gt;setName(MvcEvent::EVENT_DISPATCH_ERROR);
          $event-&gt;getApplication()-&gt;getEventManager()-&gt;triggerEvent($event);
          return false;
      });
  }
  </code></pre>
  
  <p>This way I've got the logging working. But in the frontend I'm still getting an empty page. <strong>What is wrong here and how to to get now the error page displayed?</strong></p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I think, it's a terrible logical mistake -- I'm trying to process some framework stuff <em>after</em> a <code>set_exception_handler(...)</code> call. Do I see it correctly? If so, it cannot work this way. <strong>How to solve this dilemma / What is a workaround?</strong></p>