-
-
Notifications
You must be signed in to change notification settings - Fork 0
The Config Facade
InitPHP\Config\Config is a static facade over a single, lazily created,
process-wide Library instance. It lets you
register configuration once and read it from anywhere without passing an
object around.
use InitPHP\Config\Config;
Config::setArray('site', ['url' => 'http://lvh.me']);
// ...elsewhere in the request, no wiring required:
Config::get('site.url'); // 'http://lvh.me'Every Library method is reachable statically and operates on the shared
instance:
Config::set('app.name', 'InitPHP');
Config::get('app.name'); // 'InitPHP'
Config::has('app.name'); // true
Config::remove('app.name');
Config::all();
Config::setArray('db', ['host' => 'localhost']);
Config::setFile('cache', __DIR__ . '/config/cache.php');
Config::setDir('app', __DIR__ . '/config');
Config::setClass(\App\Config\Database::class);
Config::replace(['fresh' => 'tree']);
Config::version(); // '2.0.0'The calls are forwarded through __callStatic, so the
@method annotations on the class give your IDE
full autocompletion.
Methods that would return $this on a Library return the underlying
instance from the facade, so you can keep chaining:
use InitPHP\Config\Library;
$library = Config::set('a', 1)->set('b', 2);
$library instanceof Library; // true
$library->get('a'); // 1Because the instance is process-wide, long-running workers — and
especially tests — need a way to start clean. Config::reset()
discards the shared instance; the next call creates a fresh, empty one.
final class SomeTest extends \PHPUnit\Framework\TestCase
{
protected function setUp(): void
{
Config::reset();
}
public function testSomething(): void
{
Config::set('flag', true);
$this->assertTrue(Config::get('flag'));
}
}Calling reset() when no instance exists yet is safe — it is a no-op.
Config is a pure static utility: its constructor is private, so
new Config() is a compile-time error. Use the static methods, or inject
a Library when you want an object.
| Use the facade when… | Use a Library when… |
|---|---|
| You want zero wiring and global reach. | You practice dependency injection. |
| Configuration is truly process-global. | You need isolated instances (e.g. multi-tenant). |
| Convenience outweighs testability concerns. | You want easy, reset-free test isolation. |
The facade is a convenience layer — under the hood it is the exact same
Library, so behaviour (dotted paths,
case-insensitivity, exceptions) is identical.
- The Library Object — the instance behind the facade.
- Loaders — the loader methods exposed through the facade.
- API Reference — the facade's method map.
initphp/config · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Core Concepts
Loading Configuration
Reference
Practical Guides
Migration & Help