- How does coduo/php-to-string differ from Laravel’s Str::of() for string conversion?
- This package provides a lightweight, dependency-free alternative to Str::of() with identical core functionality (e.g., casting objects/arrays to strings). Unlike Str::of(), it lacks Laravel-specific methods like `limit()` or `slug()`, making it ideal for non-Laravel PHP projects or when avoiding Illuminate dependencies. Use Str::of() if you need Laravel’s extended features.
- Can I use this package in Laravel 9+ without conflicts?
- Yes, the package has no Laravel dependencies and works seamlessly in Laravel 9+. It’s zero-configuration—just require it via Composer. For Laravel 8 or older, ensure your `composer.json` allows PHP 8.0+ (the package’s minimum requirement). No service provider or facade setup is needed.
- Does to_string() handle circular references in objects (e.g., self-referencing properties)?
- No, the package behaves like native PHP casting: circular references will cause infinite recursion and likely a stack overflow. Test edge cases like `$obj->self = $obj` before use in production. For safe handling, implement a depth limit or pre-process objects with `json_encode()` if circularity is a risk.
- Is this package safe for user-facing output (e.g., HTML/JS templates)?
- No, this package does **not** sanitize or escape strings—it’s designed for internal use only (logs, debugging, API responses). If outputting to HTML/JS, use Laravel’s `e()` or Blade’s `|e` filter, or libraries like `htmlspecialchars()`. Misuse here risks XSS vulnerabilities.
- How can I enforce consistent string conversion across a Laravel codebase?
- Replace ad-hoc `(string)$value` with `to_string($value)` via static analysis. Tools like PHPStan or PSR-12 custom rules can flag violations. Example: Add a rule to reject `(string)` casts in favor of the package. Start with a pilot phase (3–5 critical cases) to verify outputs match native casting before full adoption.
- What’s the performance impact compared to native `(string)$value` casting?
- There is **no performance impact**. The package is a thin wrapper around PHP’s native casting—identical in speed to `(string)$obj`. Use it for consistency without worrying about overhead. Benchmark only if profiling shows casting is a bottleneck (unlikely).
- Are there plans to add custom formatters (e.g., `to_string($date, 'Y-m-d')`)?
- Currently, the package mirrors PHP’s native casting behavior with no formatter extensions. If you need custom formats, use PHP’s built-in methods (e.g., `$date->format('Y-m-d')`) or Laravel’s `Str::of($date)->limit(10)` for truncation. Feature requests can be submitted to the [GitHub repo](https://github.com/coduo/php-to-string).
- Will this package break if PHP adds a built-in `to_string()` function?
- Unlikely, but monitor PHP RFCs. The package uses a class (`StringConverter`) and method (`__toString()`), not a global function, so conflicts are minimal. If PHP standardizes `to_string()`, the package could deprecate its method in favor of the native one. No action is needed now.
- How do I test that `to_string()` matches native casting (e.g., `(string) new DateTime()`)?
- Write unit tests comparing outputs: `assertEquals(to_string(new DateTime()), (string) new DateTime())`. Test edge cases like resources, arrays, and objects with `__toString()` methods. Use PHPUnit’s `expectException(RecursionError)` for circular references. Example: `assertSame(to_string($array), print_r($array, true));`
- Can I use this in non-Laravel PHP projects (e.g., Symfony, Lumen)?
- Absolutely. The package is framework-agnostic and works in any PHP 8.0+ environment. It’s useful in monolithic PHP apps, microservices, or legacy systems where consistent string conversion is needed. For Symfony, consider aliasing the class in a compiler pass or service container.