-
Notifications
You must be signed in to change notification settings - Fork 0
Recipe Database Config
Goal: keep database credentials in a dedicated PHP file, load them through the package, and read them safely when opening a connection.
// config/db.php
return [
'host' => '127.0.0.1',
'port' => 3306,
'name' => 'app',
'user' => 'app',
'pass' => '',
'charset' => 'utf8mb4',
];use InitPHP\Config\Library;
$config = new Library();
$config->setFile('db', __DIR__ . '/config/db.php');
$config->get('db.host'); // '127.0.0.1'
$config->get('db.port'); // 3306
$config->get('db.charset', 'utf8'); // 'utf8mb4'Keys are case-insensitive, so a file written with upper-case keys
('HOST' => …) is still read as db.host. See
Keys & Dotted Paths.
Read each value with a sensible default at the point of use — that is where validation belongs:
$dsn = sprintf(
'mysql:host=%s;port=%d;dbname=%s;charset=%s',
$config->get('db.host', '127.0.0.1'),
(int) $config->get('db.port', 3306),
$config->get('db.name', 'app'),
$config->get('db.charset', 'utf8mb4'),
);
$pdo = new PDO(
$dsn,
$config->get('db.user', 'root'),
$config->get('db.pass', ''),
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION],
);If you prefer object syntax, pull the whole subtree via object access (Library only):
$db = $config->db; // stdClass
$db->host; // '127.0.0.1'
$db->name; // 'app'Commit a safe db.php with defaults, and merge a git-ignored local file
on top for real credentials. To merge (rather than replace) the db
block, load the override at the root with a null name and nest the
keys under db inside the file:
// config/db.local.php (git-ignored)
return [
'db' => [
'pass' => 'realsecret', // only the keys you override
],
];use InitPHP\Config\Exceptions\ConfigException;
$config->setFile('db', __DIR__ . '/config/db.php'); // committed defaults under 'db'
try {
// null name → recursive merge at the root, so 'db.*' siblings survive
$config->setFile(null, __DIR__ . '/config/db.local.php');
} catch (ConfigException) {
// No local overrides present — defaults stand.
}
$config->get('db.pass'); // 'realsecret'
$config->get('db.host'); // '127.0.0.1' (committed default preserved)Why not load the override under the
dbname again? Loading a file under a named key (setFile('db', …)) replaces the entiredbsubtree. Recursive merging only happens for anull/empty name, which merges at the root — hence the nested['db' => …]shape above. See Loaders → setArray.
- Loaders → setFile
- Object Access
- Exceptions — handling a missing or malformed file.
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