Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Default Laravel Package

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.

View on GitHub
Deep Wiki
Context7

DefaultInterface for classes to provide standardized default instances

Frequently asked questions about Default
How does this package improve default object handling in Laravel compared to manual instantiation?
The package enforces a `DefaultInterface` contract, ensuring all classes exposing defaults follow the same pattern (e.g., `getDefault(): static`). This reduces inconsistencies in Laravel apps where defaults might otherwise be hardcoded or scattered across service providers. It also integrates cleanly with Laravel’s container for lazy-loaded defaults, avoiding eager initialization pitfalls.
Can I use this with Laravel’s service container instead of hardcoding defaults in interfaces?
Yes. The package works alongside Laravel’s container—you can bind defaults via `App::bind()` or `App::when()`, while the interface ensures classes *can* provide defaults if needed. For example, bind a `DefaultUser` in your `AppServiceProvider` and implement `getDefault()` for testing or fallback scenarios.
What Laravel versions and PHP versions are supported?
The package has no strict PHP version requirements but aligns best with Laravel 10+ (PHP 8.1+), leveraging modern features like static return types in `DefaultInterface`. For older Laravel versions, ensure your PHP version supports the interface’s syntax (e.g., PHP 7.4+ for basic static methods).
Will this work with Laravel’s testing tools like Pest or PHPUnit?
Absolutely. The interface’s `getDefault()` method is ideal for mocking or seeding test data. For example, replace `new User()` with `User::getDefault()` in tests to ensure consistent stubs. It also pairs well with Laravel’s `createMock()` for service-layer testing where defaults are critical.
How do I implement this in a Laravel service class without breaking existing code?
Start by adding the `DefaultInterface` to non-critical classes (e.g., `DefaultConfig`, `EmptyCollection`). Implement `getDefault()` with a lightweight instance (e.g., `return new static(['fallback' => true])`). Use traits or abstract classes to avoid duplicating the method across services. Laravel’s container can override these defaults in production if needed.
Could this lead to memory leaks if defaults are shared singletons?
Only if not managed properly. The package itself doesn’t enforce singleton behavior—it’s up to you to decide whether `getDefault()` returns a new instance or a shared one. For Laravel, prefer lazy-loading via the container (e.g., `App::bindIf()`) to avoid memory issues with heavy defaults like database connections.
Is there a performance impact compared to Laravel’s built-in `App::bind()`?
Minimal. The interface adds a method call (`getDefault()`), but the performance hit is negligible for most use cases. The real benefit is consistency—Laravel’s container can still override defaults, and the interface ensures all classes adhere to the same pattern. Benchmark only if defaults are called in hot paths (e.g., loop iterations).
Can I use this for API responses or Eloquent models in Laravel?
Yes, but with caution. For API responses, use it to standardize default `Resource` or `Collection` objects (e.g., empty paginators). For Eloquent models, implement `getDefault()` for testing (e.g., `User::getDefault()` returns an anonymous user), but avoid using it for production queries—Laravel’s query builder is more efficient for dynamic defaults.
What’s the difference between this package and Laravel’s `Macroable` traits or `AppServiceProvider` bindings?
This package provides a *contract* (`DefaultInterface`) to enforce defaults at the class level, while `Macroable` or service provider bindings are runtime tools. Use the interface for classes that *must* provide defaults (e.g., `DefaultConfig`), and bindings for runtime flexibility (e.g., environment-based defaults). They can coexist—bind a class to the container *and* implement the interface.
How do I enforce this pattern across a large Laravel codebase without heavy refactoring?
Start with a pilot phase: add the interface to 2–3 non-critical classes (e.g., `DefaultLogger`, `EmptyCart`). Use PHPStan or Psalm to detect missing implementations. For legacy code, create a trait (e.g., `HasDefault`) to retroactively add `getDefault()` without modifying interfaces. Document the pattern in your team’s style guide to encourage adoption.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope