Skip to content

Instantly share code, notes, and snippets.

@nasrulhazim
Last active August 11, 2022 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasrulhazim/aa11c5347e44801a28081d9fc14c334f to your computer and use it in GitHub Desktop.
Save nasrulhazim/aa11c5347e44801a28081d9fc14c334f to your computer and use it in GitHub Desktop.
Custom Exception method
<?php
namespace App\Exceptions;
use Exception;
class ContractException extends Exception
{
public static function throwIf(bool $condition, string $method, ...$args)
{
if (! $condition) {
if (method_exists(__CLASS__, $method)) {
throw self::$method(...$args);
}
throw new self("Invalid exception method $method");
}
}
public static function missingContract(string $class, string $contract)
{
return new self("$class did not implements $contract");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment