tomphp/exception-constructor-tools

A simple PHP trait which makes creating static constructors for exceptions nicer.

Installs: 41 073

Dependents: 3

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 1

Open Issues: 0

pkg:composer/tomphp/exception-constructor-tools

v2.0.0 2025-08-15 15:51 UTC

This package is not auto-updated.

Last update: 2025-10-10 16:28:16 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

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);
}