1. Go to this page and download the library: Download amirhossein5/return-error library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
amirhossein5 / return-error example snippets
enum DivisionErrors {
case DIVISION_BY_ZERO;
}
function divide(int $num, int $divideBy): int|ReturnError
{
if ($divideBy === 0) {
return new ReturnError(
message: "can't divide by zero",
type: DivisionErrors::DIVISION_BY_ZERO,
);
}
return $num / $divideBy;
}
$divisionResult = divide(20, 0);
if ($divisionResult instanceof ReturnError) {
if ($divisionResult->type === DivisionErrors::DIVISION_BY_ZERO) {
// ...
}
// ...
}
new ReturnError();
new ReturnError(message: "something went wrong");
new ReturnError(..., type: 'its_type');
new ReturnError(..., type: Enum::ENUM);
(new ReturnError())->report(); // local.ERROR: {"exception":"[object] (Exception(code: 0): at ...
(new ReturnError("with message"))->report(); // local.ERROR: message: with message {"exception...
(new ReturnError("with message", "its_type"))->report(); // local.ERROR: message: with message, type: its_type {"exception...
enum DivisionErrors: string { case DIVISION_BY_ZERO = 'division_by_zero'; }
(new ReturnError(type: DivisionErrors::DIVISION_BY_ZERO))->report(); // local.ERROR: type: DIVISION_BY_ZERO {"exception...
(new ReturnError(type: BackedDivideErrors::DIVISION_BY_ZERO))->report(); // local.ERROR: type: division_by_zero {"exception...
(new ReturnError())->report(additional: 'string'); // local.ERROR: additional: "string" {"exception...
(new ReturnError())->report(additional: ['given' => '...']); // local.ERROR: additional: {"given":"..."} {"exception...