respect/stringifier
Respect\Stringifier converts any PHP value into a readable string for logging, debugging, and error messages. Use the stringify() function or the HandlerStringifier object. Secure-by-default handling of strings in newer versions. Requires PHP 8.3+.
(string) casting or var_export().Log::debug($object) with structured, readable output (e.g., User{id:1,name:"John"}).Stringifier::stringify($validator->errors())).Cache::put(Stringifier::stringify($query), $result)).RuntimeException{message:"..." in file.php:119}).(string) which may fail or leak data).(string) Casting: Fails for objects without __toString(); inconsistent for arrays/resources.json_encode(): Produces JSON (not human-readable strings) and may fail on non-serializable objects.var_export(): Verbose and impractical for production logs/APIs.DateTime, Fiber, or Generator support).composer require respect/stringifier
spatie/php-version or a custom wrapper to abstract version-specific features.ReflectionClass polyfills).v1.x (stable, no risks).v2.0:
use Respect\Stringifier\Stringifier;
Stringifier::setOptions(['detect_classes' => false, 'detect_callables' => false]);
App\\* namespace).| Risk | Severity | Impact | Mitigation |
|---|---|---|---|
| PHP 8.3 Dependency | High | Blocks adoption for legacy stacks. | Upgrade PHP or implement polyfills. |
| Version 2.0 Security | High | Class/interface leakage in logs. | Pin to v1.x or disable sensitive detection. |
| Performance Overhead | Low | Stringification may slow critical paths. | Benchmark against (string)/json_encode(); cache results if needed. |
| Edge Case Handling | Medium | Undefined behavior for custom objects. | Test with DateTime, Closure, Resource, and nested structures. |
| Dependency Bloat | Low | Adds ~1KB to vendor directory. | Negligible; no transitive dependencies. |
| Laravel-Specific Conflicts | Low | May override existing toString() logic. |
Audit for conflicts in AppServiceProvider or custom macros. |
v2.0’s enhanced type detection (e.g., closures, fibers) worth the risk? If not, v1.x is a safer baseline.v2.0 to disable class/interface detection? Yes, via Stringifier::setOptions().Str::of() or toString() helpers? Benchmark for consistency; Stringifier handles more edge cases.Stringable interfaces or Arrayable traits? Test for conflicts in Eloquent models or collections.respect/stringifier for major releases.spatie/array-to-string or league/serializable? Stringifier is lighter and focuses on debuggable strings, not serialization.dd(), dump(), and custom debug helpers.Cache::remember()).php artisan my:command).json_encode() instead (this package outputs human-readable strings).composer require respect/stringifier:^1.0
// Before
Log::debug((string) $user);
// After
Log::debug(Stringifier::stringify($user));
User{id:1,name:"John"}).Log::* calls with Stringifier::stringify().dd()/dump() helpers to use the package.Stringifier::stringify($validator->errors()).Cache::put(Stringifier::stringify($query), $result).dispatch(new ProcessData(Stringifier::stringify($data))).v1.x:
composer require respect/stringifier:^1.0
v2.0 with safeguards:
Stringifier::setOptions([
'detect_classes' => false, // Disable class name leakage
'detect_callables' => false, // Disable callable interpretation
]);
User, Post).Collection, LengthAwarePaginator).ValidationException, HttpException).DateTime, Closure, Resource).User{id:1,posts:[Post{...}]}).Collection, LengthAwarePaginator, and custom iterators.FormRequest error messages.Monolog and Laravel’s Log facade.How can I help you explore Laravel packages today?