tomphp / exception-constructor-tools
A simple PHP trait which makes creating static constructors for exceptions nicer.
Package info
github.com/tomphp/exception-constructor-tools
pkg:composer/tomphp/exception-constructor-tools
v2.0.0
2025-08-15 15:51 UTC
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: 2026-02-27 18:39:03 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); }