PHPackages                             respect/stringifier - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. respect/stringifier

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

respect/stringifier
===================

Converts any value to a string

3.0.0(4mo ago)2522.0M—2.5%27MITPHPPHP ^8.3CI passing

Since Dec 28Pushed 1w ago2 watchersCompare

[ Source](https://github.com/Respect/Stringifier)[ Packagist](https://packagist.org/packages/respect/stringifier)[ RSS](/packages/respect-stringifier/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (7)Versions (8)Used By (7)

Respect\\Stringifier
====================

[](#respectstringifier)

[![Build Status](https://camo.githubusercontent.com/b528a8d4b945b7fb17e4d22ed01aa96e464fa701d44df2df1d931b1cef1bb558/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f526573706563742f537472696e6769666965722f636f6e74696e756f75732d696e746567726174696f6e2e796d6c3f6272616e63683d6d6173746572267374796c653d666c61742d737175617265)](https://github.com/Respect/Stringifier/actions/workflows/continuous-integration.yml)[![Code Coverage](https://camo.githubusercontent.com/089f2097eba4ad72540fca186dd7ec6c398da9532b47fc91adbe31702b82de86/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f526573706563742f537472696e6769666965723f7374796c653d666c61742d737175617265)](https://codecov.io/gh/Respect/Stringifier)[![Latest Stable Version](https://camo.githubusercontent.com/5557f2ba93f42562ad65f2e2c72d9f017db6da0cae1286ec30fcef1d9302fbd0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726573706563742f737472696e6769666965722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/respect/stringifier)[![Total Downloads](https://camo.githubusercontent.com/63a1ce339d62753c25d581d54de7462620e53da5da325459710d8c6fc7847d83/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726573706563742f737472696e6769666965722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/respect/stringifier)[![License](https://camo.githubusercontent.com/2c0418b1a00fd43945136a50846e374a1acd9893916dac5f18a9683664f35f8a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f726573706563742f737472696e6769666965722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/respect/stringifier)

Converts any PHP value into a string.

Installation
------------

[](#installation)

Package is available on [Packagist](https://packagist.org/packages/respect/stringifier), you can install it using [Composer](http://getcomposer.org).

```
composer require respect/stringifier
```

This library requires PHP &gt;= 8.3.

IMPORTANT: Be careful with version 2.0
--------------------------------------

[](#important-be-careful-with-version-20)

If you are using version 2.0, please be aware of security concerns related to information leakage.

1. **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.
2. **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.

Usage
-----

[](#usage)

Below a quick guide of how to use the library.

### Using as a function

[](#using-as-a-function)

```
echo Respect\Stringifier\stringify($value);
```

### Using as an object

[](#using-as-an-object)

```
use Respect\Stringifier\HandlerStringifier;

$stringifier = HandlerStringifier::create();

echo $stringifier->stringify($value);
```

### Examples

[](#examples)

```
use function Respect\Stringifier\stringify;

echo stringify('string') . PHP_EOL;
// "string"

echo stringify(implode(PHP_EOL, ['Multi-line', 'string'])) . PHP_EOL;
// "Multi-line\nstring"

echo stringify(1) . PHP_EOL;
// 1

echo stringify(0.5) . PHP_EOL;
// 0.5

echo stringify(true) . PHP_EOL;
// `true`

echo stringify(false) . PHP_EOL;
// `false`

echo stringify(null) . PHP_EOL;
// `null`

echo stringify(INF) . PHP_EOL;
// `INF`

echo stringify(-INF) . PHP_EOL;
// `-INF`

echo stringify(acos(8)) . PHP_EOL;
// `NaN`

echo stringify([1, 2, 3]) . PHP_EOL;
// `[1, 2, 3]`

echo stringify(['foo' => true, 'bar' => 42, 'baz' => ['qux' => INF, 'quux' => null]]) . PHP_EOL;
// `["foo": true, "bar": 42, "baz": ["qux": INF, "quux": null]]`

echo stringify(tmpfile()) . PHP_EOL;
// `resource `

echo stringify(new WithProperties()) . PHP_EOL;
// `WithProperties { +$publicProperty=true #$protectedProperty=42 -$privateProperty="something" }`

echo stringify(new WithUninitializedProperties()) . PHP_EOL;
// `WithUninitializedProperties { +$uninitializedProperty=*uninitialized* }`

echo stringify(new class { public int $property = 42; }) . PHP_EOL;
// `class { +$property=42 }`

echo stringify(new class extends WithProperties { }) . PHP_EOL;
// `WithProperties@anonymous { +$publicProperty=true #$protectedProperty=42 }`

echo stringify(fn(int $foo): string => '') . PHP_EOL;
// `Closure { fn(int $foo): string }`

echo stringify(new DateTime()) . PHP_EOL;
// `DateTime { 2023-04-21T11:29:03+00:00 }`

echo stringify(new DateTimeImmutable()) . PHP_EOL;
// `DateTimeImmutable { 2023-04-21T11:29:03+00:00 }`

echo stringify(new Fiber('strlen')) . PHP_EOL;
// `Fiber { strlen(string $string): int }`

echo stringify((static fn(int $number) => yield $number)(7)) . PHP_EOL;
// `Generator { current() => 7 }`

echo stringify(new ConcreteIterator()) . PHP_EOL;
// `ConcreteIterator { current() => 1 }`

echo stringify(new ConcreteStringable()) . PHP_EOL;
// `ConcreteStringable { __toString() => "This is the return of __toString()" }`

echo stringify(new ConcreteJsonSerializable()) . PHP_EOL;
// `ConcreteJsonSerializable { jsonSerialize() => {"0":1,"1":2,"2":3,"foo":true} }`

echo stringify(new WithDebugInfo()) . PHP_EOL;
// `WithDebugInfo { __debugInfo() => ["info": "This is the return of __debugInfo()"] }`

echo stringify(new ArrayObject([1, 2, 3])) . PHP_EOL;
// `ArrayObject { getArrayCopy() => [1, 2, 3] }`

echo stringify(new RuntimeException()) . PHP_EOL;
// `RuntimeException { in file.php:119 }`

echo stringify(new InvalidArgumentException('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](tests/integration).

### Custom stringifiers

[](#custom-stringifiers)

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`.

```
use Respect\Stringifier\DumpStringifier;
use Respect\Stringifier\Handler;
use Respect\Stringifier\Handlers\CompositeHandler;
use Respect\Stringifier\HandlerStringifier;
use Respect\Stringifier\Stringify;

$compositeHandler = CompositeHandler::create();
$compositeHandler->prependStringifier(new class implements Handler {
    public function handle(mixed $raw, int $depth): ?string
    {
        if (is_object($raw) && method_exists($raw, 'toString')) {
            return $raw->toString();
        }

        return null;
    }
});

$stringifier = new HandlerStringifier($compositeHandler, new DumpStringifier());

echo $stringifier->stringify(new class {
    public function toString(): 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.

###  Health Score

67

↑

FairBetter than 100% of packages

Maintenance89

Actively maintained with recent releases

Popularity57

Moderate usage in the ecosystem

Community18

Small or concentrated contributor base

Maturity83

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~490 days

Recently: every ~253 days

Total

7

Last Release

120d ago

Major Versions

0.2.0 → 1.0.02023-04-12

1.0.0 → 2.0.02023-04-22

2.0.x-dev → 3.0.02026-01-19

PHP version history (3 changes)0.1.0PHP &gt;=7.1

1.0.0PHP ^8.1

3.0.0PHP ^8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/202642?v=4)[Alexandre Gomes Gaigalas](/maintainers/alganet)[@alganet](https://github.com/alganet)

![](https://avatars.githubusercontent.com/u/154023?v=4)[Henrique Moody](/maintainers/henriquemoody)[@henriquemoody](https://github.com/henriquemoody)

---

Top Contributors

[![henriquemoody](https://avatars.githubusercontent.com/u/154023?v=4)](https://github.com/henriquemoody "henriquemoody (75 commits)")

---

Tags

data-manipulationphpstringstringifierstringifyrespectstringifierstringify

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/respect-stringifier/health.svg)

```
[![Health](https://phpackages.com/badges/respect-stringifier/health.svg)](https://phpackages.com/packages/respect-stringifier)
```

###  Alternatives

[respect/config

A powerful, small, deadly simple configurator and dependency injection container made to be easy.

10211.4k1](/packages/respect-config)[focuslabllc/craft-cheat-sheet

A fast and customized set of instantly usable Field code samples.

21021.3k](/packages/focuslabllc-craft-cheat-sheet)[arcanedev/gravatar

A library providing easy gravatar integration/generation (Laravel supported).

1986.8k](/packages/arcanedev-gravatar)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
