php-standard-library/default
Provides a DefaultInterface for PHP classes to expose standardized “default” instances. Helps ensure consistent default construction across libraries and apps with a simple, shared contract.
DefaultInterface package enforces a standardized way to provide default instances for classes, which aligns well with Laravel’s dependency injection (DI) and service container patterns. It could reduce boilerplate for default object initialization (e.g., empty collections, default configurations, or fallback models) and improve consistency in service layer implementations.App::bindIf() or Singleton bindings), but this package could formalize a contract-driven approach, making it easier to enforce defaults across microservices or modular applications.User or Order stubs for testing/mocking).DefaultInterface is a simple contract (public function getDefault(): static), making it easy to retroactively add to existing classes without major refactoring.App::bind() or App::when().Cache::rememberForever() could use a default CacheItem).User::getDefault() instead of new User()).static return types in the interface.getDefault() calls.getDefault() instead of direct instantiation.App::bind('default-user', fn() => new User()))?getDefault() be implemented? (Eager vs. lazy initialization?)Macroable traits or AppServiceProvider bindings achieve the same goal?Default::user())?AppServiceProvider or modular providers.RepositoryInterface or ServiceInterface.Resource or Collection responses.Subscription stubs.DefaultConfig, EmptyCollection).class User implements DefaultInterface {
public function getDefault(): static {
return new static([
'name' => 'Anonymous',
'email' => null,
]);
}
}
AppServiceProvider:
$this->app->bind('default-user', fn() => User::getDefault());
new User() with app('default-user') where appropriate.DefaultInterface to shared contracts (e.g., RepositoryInterface).createMock(User::class) with User::getDefault() in tests.Model stubs (e.g., User::getDefault() for seeders).| Step | Priority | Effort | Dependencies |
|---|---|---|---|
Add DefaultInterface to pilot classes |
High | Low | None |
Bind defaults in AppServiceProvider |
Medium | Medium | Pilot classes |
Update tests to use getDefault() |
Low | High | Pilot classes |
| Enforce via interfaces in shared contracts | Medium | Medium | Laravel’s DI system |
| Document patterns for team adoption | Low | Low | All prior steps |
DefaultInterface to every class may feel redundant.HasDefaultTrait).getDefault()).getDefault() implementations.boot() instead of register()).new or container bindings).getDefault() call overhead.getDefault() calls).Cache::remember()).| Failure Scenario | Impact | Mitigation |
|---|---|---|
getDefault() returns invalid state |
Runtime errors in dependent code | Validate defaults in constructor. |
| Defaults not bound in container | UndefinedMethod errors |
Use method_exists() checks. |
| Defaults shared across requests | Data corruption | Ensure defaults are immutable or request-scoped. |
| Over-aggressive adoption | Unnecessary complexity | Limit scope to critical classes. |
CONTRIBUTING.md or internal wiki on when/why to use DefaultInterface.User, Config, Collection).DefaultInterface adoption for new classes where applicable.How can I help you explore Laravel packages today?