Converts any PHP value into a string.
Package is available on Packagist, you can install it using Composer.
composer require respect/stringifierThis library requires PHP >= 8.3.
If you are using version 2.0, please be aware of security concerns related to information leakage.
- Class/Interface/Enum Detection: Version 2.0 would automatically detect and format strings that matched internal class, interface, or enum names. This could expose your application's internal architecture.
- Callable String Detection: Version 2.0 would interpret strings and arrays as callables by default, potentially exposing sensitive data. .
If you're not stringifiying strings coming from and end-user, you're not at risk. However, later versions changed this to a "secure-by-default" approach, assuming that strings may come from untrusted sources.
Below a quick guide of how to use the library.
echoRespect\Stringifier\stringify($value);useRespect\Stringifier\HandlerStringifier;
$stringifier = HandlerStringifier::create();
echo$stringifier->stringify($value);usefunctionRespect\Stringifier\stringify;
echostringify('string') . PHP_EOL;
// "string"echostringify(implode(PHP_EOL, ['Multi-line', 'string'])) . PHP_EOL;
// "Multi-line\nstring"echostringify(1) . PHP_EOL;
// 1echostringify(0.5) . PHP_EOL;
// 0.5echostringify(true) . PHP_EOL;
// `true`echostringify(false) . PHP_EOL;
// `false`echostringify(null) . PHP_EOL;
// `null`echostringify(INF) . PHP_EOL;
// `INF`echostringify(-INF) . PHP_EOL;
// `-INF`echostringify(acos(8)) . PHP_EOL;
// `NaN`echostringify([1, 2, 3]) . PHP_EOL;
// `[1, 2, 3]`echostringify(['foo' => true, 'bar' => 42, 'baz' => ['qux' => INF, 'quux' => null]]) . PHP_EOL;
// `["foo": true, "bar": 42, "baz": ["qux": INF, "quux": null]]`echostringify(tmpfile()) . PHP_EOL;
// `resource <stream>`echostringify(newWithProperties()) . PHP_EOL;
// `WithProperties { +$publicProperty=true #$protectedProperty=42 -$privateProperty="something" }`echostringify(newWithUninitializedProperties()) . PHP_EOL;
// `WithUninitializedProperties { +$uninitializedProperty=*uninitialized* }`echostringify(newclass { publicint$property = 42; }) . PHP_EOL;
// `class { +$property=42 }`echostringify(newclassextends WithProperties { }) . PHP_EOL;
// `WithProperties@anonymous { +$publicProperty=true #$protectedProperty=42 }`echostringify(fn(int$foo): string => '') . PHP_EOL;
// `Closure { fn(int $foo): string }`echostringify(newDateTime()) . PHP_EOL;
// `DateTime { 2023-04-21T11:29:03+00:00 }`echostringify(newDateTimeImmutable()) . PHP_EOL;
// `DateTimeImmutable { 2023-04-21T11:29:03+00:00 }`echostringify(newFiber('strlen')) . PHP_EOL;
// `Fiber { strlen(string $string): int }`echostringify((staticfn(int$number) => yield$number)(7)) . PHP_EOL;
// `Generator { current() => 7 }`echostringify(newConcreteIterator()) . PHP_EOL;
// `ConcreteIterator { current() => 1 }`echostringify(newConcreteStringable()) . PHP_EOL;
// `ConcreteStringable { __toString() => "This is the return of __toString()" }`echostringify(newConcreteJsonSerializable()) . PHP_EOL;
// `ConcreteJsonSerializable { jsonSerialize() => {"0":1,"1":2,"2":3,"foo":true} }`echostringify(newWithDebugInfo()) . PHP_EOL;
// `WithDebugInfo { __debugInfo() => ["info": "This is the return of __debugInfo()"] }`echostringify(newArrayObject([1, 2, 3])) . PHP_EOL;
// `ArrayObject { getArrayCopy() => [1, 2, 3] }`echostringify(newRuntimeException()) . PHP_EOL;
// `RuntimeException { in file.php:119 }`echostringify(newInvalidArgumentException('This is the exception message')) . PHP_EOL;
// `InvalidArgumentException { "This is the exception message" in file.php:112 }`To see more examples of how to use the library check the integration tests.
Stringifier library is extensible, you can create your own stringifiers and handlers. Considering the internal design,
it's best to create an implementation of Handler, and then use it to create a HandlerStringifier.
useRespect\Stringifier\DumpStringifier;
useRespect\Stringifier\Handler;
useRespect\Stringifier\Handlers\CompositeHandler;
useRespect\Stringifier\HandlerStringifier;
useRespect\Stringifier\Stringify;
$compositeHandler = CompositeHandler::create();
$compositeHandler->prependStringifier(newclassimplements Handler {
publicfunctionhandle(mixed$raw, int$depth): ?string
{
if (is_object($raw) && method_exists($raw, 'toString')) {
return$raw->toString();
}
returnnull;
}
});
$stringifier = newHandlerStringifier($compositeHandler, newDumpStringifier());
echo$stringifier->stringify(newclass {
publicfunctiontoString(): string
{
return'Hello, world!';
}
});
// Hello, world!The DumpStringifier is a fallback stringifier that uses print_r-like output. You can replace it with any other.