tomphp / exception-constructor-tools
A simple PHP trait which makes creating static constructors for exceptions nicer.
Installs: 41 054
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: >=8.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.85
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^9.6 || ^7.5 || ^11.5
- rector/rector: ^2.1
- squizlabs/php_codesniffer: ^3.9
This package is not auto-updated.
Last update: 2025-08-15 15:54:09 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); }