- var_dump: documentation ( source)
- define: documentation ( source)
<?php
define('MAX_ATTEMPTS', 5);
class NetworkException extends Exception { }
class FetchDataException extends Exception {
private $surpressed = [];
public function addSuppressed(Throwable $e): void {
$this->surpressed[] = $e;
}
public function getSuppressed(): array {
return $this->surpressed;
}
}
function fetchDataOverNetwork() {
$networkExceptions = [];
for ($i = 0; $i < MAX_ATTEMPTS; $i++) {
try {
// Some operation that may throw
throw new NetworkException();
}
catch (NetworkException $ne) {
$networkExceptions[] = $ne;
}
}
$fdException = new FetchDataException("Failed to get data");
foreach ($networkExceptions as $networkException) {
$fdException->addSuppressed($networkException);
}
throw $fdException;
}
try {
fetchDataOverNetwork();
}
catch(FetchDataException $fde) {
var_dump($fde->getSuppressed());
}