tomphp / exception-constructor-tools
A simple PHP trait which makes creating static constructors for exceptions nicer.
Installs: 40 781
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^5.6|^7.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-11-09 21:06:47 UTC
README
A simple PHP trait which makes creating static constructors for exceptions nicer.
Installation
$ composer require tomphp/exception-constructor-tools
Usage
Define your exception:
<?php use TomPHP\ExceptionConstructorTools\ExceptionConstructorTools; class MyExceptionClass extends \RuntimeException { use ExceptionConstructorTools; public static function forEntity($entity) { return self::create( 'There was an error with an entity of type %s with value of %s.', [ self::typeToString($entity) self::valueToString($entity) ] ); } }
Throw your exception:
if ($errorOccurred) { throw MyExceptionClass::forEntity($entity); }