3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Debug; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Debug\Exception\FlattenException; use Symfony\Component\Debug\Exception\OutOfMemoryException; if (!defined('ENT_SUBSTITUTE')) { define('ENT_SUBSTITUTE', 8); } /** * ExceptionHandler converts an exception to a Response object. * * It is mostly useful in debug mode to replace the default PHP/XDebug * output with something prettier and more useful. * * As this class is mainly used during Kernel boot, where nothing is yet * available, the Response content is always HTML. * * @author Fabien Potencier <fabien@symfony.com> */ class ExceptionHandler { private $debug; private $charset; private $handler; private $caughtOutput = 0; public function __construct($debug = true, $charset = 'UTF-8') { $this->debug = $debug; $this->charset = $charset; } /** * Registers the exception handler. * * @param bool $debug * * @return ExceptionHandler The registered exception handler */ public static function register($debug = true) { $handler = new static($debug); set_exception_handler(array($handler, 'handle')); return $handler; } /** * Sets a user exception handler. * * @param callable $handler An handler that will be called on Exception * * @return callable|null The previous exception handler if any */ public function setHandler($handler) { if (isset($handler) && !is_callable($handler)) { throw new \LogicException('The exception handler must be a valid PHP callable.'); } $old = $this->handler; $this->handler = $handler; return $old; } /** * {@inheritdoc} * * Sends a response for the given Exception. * * If you have the Symfony HttpFoundation component installed, * this method will use it to create and send the response. If not, * it will fallback to plain PHP functions. * * @see sendPhpResponse * @see createResponse */ public function handle(\Exception $exception) { if ($exception instanceof OutOfMemoryException) { $this->sendPhpResponse($exception); return; } // To be as fail-safe as possible, the exception is first handled // by our simple exception handler, then by the user exception handler. // The latter takes precedence and any output from the former is cancelled, // if and only if nothing bad happens in this handling path. $caughtOutput = 0; $this->caughtOutput = false; ob_start(array($this, 'catchOutput')); try { if (class_exists('Symfony\Component\HttpFoundation\Response')) { $response = $this->createResponse($exception); $response->sendHeaders(); $response->sendContent(); } else { $this->sendPhpResponse($exception); } } catch (\Exception $e) { // Ignore this $e exception, we have to deal with $exception } if (false === $this->caughtOutput) { ob_end_clean(); } if (isset($this->caughtOutput[0])) { ob_start(array($this, 'cleanOutput')); echo $this->caughtOutput; $caughtOutput = ob_get_length(); } $this->caughtOutput = 0; if (!empty($this->handler)) { try { call_user_func($this->handler, $exception); if ($caughtOutput) { $this->caughtOutput = $caughtOutput; } ob_end_flush(); } catch (\Exception $e) { if (!$caughtOutput) { // All handlers failed. Let PHP handle that now. throw $exception; } } } } /** * Sends the error associated with the given Exception as a plain PHP response. * * This method uses plain PHP functions like header() and echo to output * the response. * * @param \Exception|FlattenException $exception An \Exception instance */ public function sendPhpResponse($exception) { echo $exception->getMessage(); } /** * @internal */ public function catchOutput($buffer) { $this->caughtOutput = $buffer; return ''; } /** * @internal */ public function cleanOutput($buffer) { if ($this->caughtOutput) { // use substr_replace() instead of substr() for mbstring overloading resistance $cleanBuffer = substr_replace($buffer, '', 0, $this->caughtOutput); if (isset($cleanBuffer[0])) { $buffer = $cleanBuffer; } } return $buffer; } } ExceptionHandler::register(); throw new \Exception('foo msg');
Output for 5.3.0 - 5.3.29, 5.4.10 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
foo msg
Output for 5.4.0 - 5.4.9
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/nWEP3 on line 12
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/nWEP3 on line 12
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/nWEP3 on line 12
Process exited with code 255.

preferences:
206.63 ms | 401 KiB | 329 Q