- How does respect/parameter differ from Laravel’s built-in `app()->bind()` for dependency injection?
- respect/parameter resolves dependencies automatically via type hints (e.g., `UserRepository $repo`), eliminating manual `app()->bind()` calls. It’s ideal for PSR-11 containers and reduces boilerplate, while Laravel’s `bind()` requires explicit class-to-class mappings. Use it for interfaces or abstract classes where type-based resolution suffices.
- Can I use respect/parameter in Laravel 8/9/10 without conflicts?
- Yes, it’s fully compatible with Laravel 5.5+ (PSR-11 compliant). No conflicts arise with Laravel’s Service Container, but avoid mixing manual `bind()` calls for the same types. For Laravel-specific interfaces (e.g., `Authenticatable`), you may need custom resolvers or explicit bindings.
- Will this break existing `app()->make()` or `resolve()` calls in my app?
- No, respect/parameter works alongside Laravel’s `app()` helper. It only enhances type-based resolution—existing `make()` or `resolve()` calls remain unchanged. However, replace `app()->make(UserRepository::class)` with constructor injection (e.g., `public function __construct(UserRepository $repo)`) for automatic resolution.
- How do I handle circular dependencies with respect/parameter?
- Circular dependencies are resolved the same way as in Laravel’s container: use explicit bindings with `when()` or `needs()` for lazy resolution. Avoid binding concrete classes to interfaces in circular paths. Test with `php artisan container:dump` to debug binding conflicts.
- Does this package support Laravel’s contextual binding or tagging?
- Yes, respect/parameter integrates with Laravel’s contextual binding (e.g., `bindWhen()`) and tagging. Use it to resolve dependencies dynamically, like `app()->tagged('repositories')`. However, contextual bindings must still be registered explicitly in your Service Providers.
- How does this affect unit testing (PHPUnit/Pest) with mocked dependencies?
- It simplifies testing by resolving dependencies via the container. Mock the container directly (e.g., `container->shouldReceive('make')->andReturn($mock)`) instead of manually binding mocks. For Pest, use `partialMock()` on the container to control resolutions.
- What’s the performance impact of using type-based resolution vs. manual binding?
- Negligible. PSR-11 containers (like Laravel’s) are optimized for type-based resolution. The overhead is identical to `app()->make()`, but you avoid repetitive `bind()` calls. Benchmark with `php artisan tinker` to compare resolutions in your specific use case.
- Can I use respect/parameter for non-class types (e.g., closures, arrays) in Laravel?
- No, it only resolves class/interface types via PHP’s type hints. For closures or arrays, stick to Laravel’s `bind()` or `app()->makeWith()` methods. Custom resolvers can extend functionality, but they require manual implementation.
- How do I migrate from manual `bind()` calls to respect/parameter?
- Start by replacing `app()->bind(Interface::class, Implementation::class)` with constructor injection (e.g., `public function __construct(Interface $dep)`). Use `php artisan container:dump` to audit existing bindings. For ambiguous types, add explicit bindings or use `bindIf()` for conditional resolution.
- Are there alternatives to respect/parameter for Laravel DI?
- Laravel’s native Service Container (PSR-11) and packages like `php-di/php-di` (PHP-DI) offer similar functionality. PHP-DI is more opinionated but supports autowiring out of the box. For minimalism, stick to Laravel’s `app()` helper or `resolve()` method. Choose respect/parameter if you prefer PSR-11 compliance without framework lock-in.