tomphp / exception-constructor-tools
一个简单的PHP特性,使创建异常的静态构造函数更加方便。
v1.0.0
2016-09-09 09:30 UTC
Requires
- php: ^5.6|^7.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-14 20:09:25 UTC
README
一个简单的PHP特性,使创建异常的静态构造函数更加方便。
安装
$ composer require tomphp/exception-constructor-tools
用法
定义你的异常
<?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) ] ); } }
抛出你的异常
if ($errorOccurred) { throw MyExceptionClass::forEntity($entity); }