<?php declare(strict_types=1);
class CompositeException extends \Exception
{
/** @var array */
private $throwables = [];
public function __construct( \Throwable ...$throwables )
{
$this->throwables = $throwables;
parent::__construct((string)$this);
}
public function __toString() : string
{
$str = '';
foreach ( $this->throwables as $throwable )
{
$str .= "{$throwable->getMessage()}\n\n";
$str .= "{$throwable->getTraceAsString()}\n\n";
}
return $str;
}
}
$first = new \Exception('First');
$second = new \Exception('Second');
$third = new \Exception('Third');
$previous = new CompositeException($first, $second, $third);
throw new \Exception('Aggregate', 0, $previous);
PHP Fatal error: Uncaught CompositeException: First
#0 {main}
Second
#0 {main}
Third
#0 {main}
in /Users/hollodotme/Sites/holloDotMe/tests/ex.php:36
Stack trace:
#0 {main}
Next Exception: Aggregate in /Users/hollodotme/Sites/holloDotMe/tests/ex.php:38
Stack trace:
#0 {main}
thrown in /Users/hollodotme/Sites/holloDotMe/tests/ex.php on line 38
Fatal error: Uncaught CompositeException: First
#0 {main}
Second
#0 {main}
Third
#0 {main}
in /Users/hollodotme/Sites/holloDotMe/tests/ex.php:36
Stack trace:
#0 {main}
Next Exception: Aggregate in /Users/hollodotme/Sites/holloDotMe/tests/ex.php on line 38
Exception: Aggregate in /Users/hollodotme/Sites/holloDotMe/tests/ex.php on line 38
Call Stack:
0.0002 359032 1. {main}() /Users/hollodotme/Sites/holloDotMe/tests/ex.php:0