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.
Below a quick guide of how to use the library.
echoRespect\Stringifier\stringify($value);$stringify = Respect\Stringifier\Stringify::createDefault(); // with the `value` methodecho$stringify->value($value); // with the `__invoke` methodecho$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(BasicEnumeration::FOO) . PHP_EOL; // `BasicEnumeration::FOO`echostringify(BackedEnumeration::QUX) . PHP_EOL; // `BackedEnumeration::QUX`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('chr') . PHP_EOL; // `chr(int $codepoint): string`echostringify([newWithMethods(), 'publicMethod']) . PHP_EOL; // `WithMethods->publicMethod(Iterator&Countable $parameter): ?static`echostringify('WithMethods::publicStaticMethod') . PHP_EOL; // `WithMethods::publicStaticMethod(int|float $parameter): void`echostringify(['WithMethods', 'publicStaticMethod']) . PHP_EOL; // `WithMethods::publicStaticMethod(int|float $parameter): void`echostringify(newWithInvoke()) . PHP_EOL; // `WithInvoke->__invoke(int $parameter = 0): never`echostringify(staticfn(int$foo): string => '') . PHP_EOL; // `function (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 }`echostringify(Traversable::class) . PHP_EOL; // `Traversable`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 use them with the Stringify class.
useRespect\Stringifier\Stringifier; useRespect\Stringifier\Stringifiers\CompositeStringifier; useRespect\Stringifier\Stringify; $compositeStringifier = CompositeStringifier::createDefault(); $compositeStringifier->prependStringifier(newclassimplements Stringifier{publicfunctionstringify(mixed$raw, int$depth): ?string{if (is_object($raw) && method_exists($raw, 'toString')){return$raw->toString()} returnnull} }); $stringify = newStringify($compositeStringifier); echo$stringify->value(newclass{publicfunctiontoString(): string{return'Hello, world!'} }); // Hello, world!