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
Php Settings Container

Php Settings Container Laravel Package

chillerlan/php-settings-container

Lightweight settings container for PHP that decouples configuration logic from your application. Provides a SettingsContainerInterface with property-hook style access (for PHP < 8.4). Not a dependency injection container.

View on GitHub
Deep Wiki
Context7

A container class for immutable settings objects. Not a DI container.

Frequently asked questions about Php Settings Container
Can I use this package in Laravel to replace the built-in config() helper for immutable settings?
Yes, this package is ideal for replacing Laravel’s mutable `config()` for domain-specific, immutable settings like API clients or feature flags. Use it alongside Laravel’s config() for a hybrid approach, or migrate critical configs to typed `SettingsContainer` objects. The package integrates with Laravel’s cache drivers via serialization.
What’s the minimum Laravel version required for full functionality?
The package requires **PHP 8.4+** for full property hooks support, which may limit Laravel versions (Laravel 10+). For older Laravel versions, use the polyfill or stick to manual getters/setters. Test thoroughly with PHPStan to catch trait conflicts or invalid property access.
How do I migrate existing Laravel config arrays into a SettingsContainer?
Use the `fromArray()` or `fromJSON()` methods to backfill Laravel’s config() data into a `SettingsContainer`. For example: `$settings = new SettingsContainerAbstract(['app.name' => config('app.name')]);`. For large migrations, consider a hybrid phase where critical configs are moved first.
Does this package work with Laravel’s service container for dependency injection?
Yes, you can bind `SettingsContainer` instances to Laravel’s service container via traits or factories. However, this package is **not a DI container**—it’s designed for immutable, typed settings. Use it alongside Laravel’s DI for services that need runtime modification.
How do I handle runtime validation of settings (e.g., sanitizing API keys) without breaking immutability?
Override the `set_*` or `get_*` methods in your settings class or use traits to add validation logic. For example, a `set_apiKey()` method can sanitize input before assignment. Immutability is enforced by design, so updates require reconstructing the container with new data.
Will this package impact performance in high-throughput Laravel APIs?
Serialization/deserialization overhead is minimal for most use cases, but test with your specific workload. Compare against Laravel’s cached `config()` by benchmarking both. The package supports `JsonSerializable` and `Serializable`, so caching (e.g., Redis) remains efficient.
How do I mock SettingsContainer in Laravel unit tests?
Use PHP’s built-in mocking or Laravel’s `Mockery` to create anonymous classes implementing `SettingsContainerInterface`. For trait-heavy setups, test with `phpstan` using the provided `rules-magic-access.neon` to catch conflicts. Example: `$mock = $this->mock(SettingsContainerAbstract::class);`.
Are there alternatives to this package for Laravel that don’t require PHP 8.4?
For PHP < 8.4, consider `vlucas/phpdotenv` for environment variables or Laravel’s built-in `config()` for mutable settings. For typed configs, `spatie/laravel-settings` (though Laravel-specific) or `league/config` (framework-agnostic) are alternatives. This package’s polyfill works but lacks property hooks.
How do I enable property hooks for PHP < 8.4 without breaking Laravel’s magic methods?
Prefix custom methods with `_` (e.g., `_get_foo()`) to avoid conflicts with Laravel’s `__get()` or `ArrayAccess`. Use the `#[ThrowOnInvalidProperty]` attribute to fail fast on invalid access. For complex setups, test with `phpstan` to detect trait collisions early.
Can I use this package to manage environment variables in Laravel?
While possible, this package is optimized for **application settings** (e.g., API keys, feature flags) rather than environment variables. For `.env` management, pair it with `vlucas/phpdotenv` or Laravel’s `env()` helper. Load environment variables into the container via `fromArray()` during bootstrap.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4