A collection of functions for formatting PHP values and code elements into human-readable strings. Useful for error messages, exceptions, and debugging output.
composer require typhoon/formatterAll functions are in the Typhoon\Formatter namespace.
Formats any PHP value.
format(null); // null
format(true); // true
format(42); // 42
format("it's fine"); // 'it\'s fine'
format([1, 2, 3]); // list{1, 2, 3}
format(['a' => 1, 'b' => 2]); // array{a: 1, b: 2}
format((object)['x' => 1]); // object{x: 1}
format(Status::Active); // Status::Active
format(new MyClass()); // MyClass
format(fn() => null); // function@src/foo.php:12()Formats a class name. Anonymous classes get a readable file:line label.
formatClass(MyClass::class); // MyClass
formatClass(new MyClass()); // MyClass
formatClass(new class {}); // class@src/foo.php:42Formats a callable.
formatFunction('strlen'); // strlen()
formatFunction('MyClass::myMethod'); // MyClass::myMethod()
formatFunction([MyClass::class, 'myMethod']); // MyClass::myMethod()
formatFunction(fn() => null); // function@src/foo.php:12()
formatFunction(new MyInvokable()); // MyInvokable()Formats a parameter reference by index or name.
formatParameter('myFunction', 'value'); // myFunction($value)
formatParameter('myFunction', 0); // myFunction($0)Formats a property reference.
formatProperty(MyClass::class, 'name'); // MyClass::$name
formatProperty(new MyClass(), 'name'); // MyClass::$nameConvenient wrappers around the functions above.
| Function | Example output |
|---|---|
formatReflectedClass(\ReflectionClass $class) |
MyClass |
formatReflectedFunction(\ReflectionFunctionAbstract $function) |
MyClass::myMethod() |
formatReflectedParameter(\ReflectionParameter $parameter) |
MyClass::myMethod($param) |
formatReflectedProperty(\ReflectionProperty $property) |
MyClass::$name |
formatReflectedType(?\ReflectionType $type) |
string|int, ?Foo, A&B |