PHP code example of nabeghe / confix
1. Go to this page and download the library: Download nabeghe/confix library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nabeghe / confix example snippets
use Nabeghe\Confix\Confix;
abstract class Settings extends Confix
{
#[Override]
protected static function getDirectory(): string
{
// TODO: Return the path to the config files directory without a trailing slash.
}
}
use Nabeghe\Confix\Confix;
/**
* @method static string name(string $value = 0)
* @method static bool debug(bool $value = 0)
* @method static string version(string $value = 0)
*/
class AppConfig extends Confix
{
const string NAME = 'app'; // → config/app.php
const array DEFAULTS = [
'name' => 'My Application',
'debug' => false,
'version' => '1.0.0',
];
protected static array $_; // declares file ownership
}
/**
* @method static string driver(string $value = 0)
* @method static int ttl(int $value = '')
*/
class CacheConfig extends Confix
{
const string NAME = 'cache'; // key inside app.php
const ?string PARENT = AppConfig::class;
const array DEFAULTS = [
'driver' => 'file',
'ttl' => 600,
];
// No protected static array $_ here!
}
return [
'name' => 'My Application',
'cache' => [
'driver' => 'redis',
'ttl' => 3600,
],
];
/**
* @method static string name(string $value = 0)
* @method static bool debug(bool $value = 0)
* @method static int version(string $value = 0)
*/
class AppConfig extends Confix { ... }
class InfraConfig extends Confix
{
const string NAME = 'infra';
const bool NESTED = true; // important
const array DEFAULTS = [
'db' => [
'host' => 'localhost',
'port' => 3306,
],
];
protected static array $_;
}
// Read
InfraConfig::get('db.host'); // 'localhost'
InfraConfig::get('db.port'); // 3306
// Write
InfraConfig::set('db.host', '10.0.0.1');
// Forget
InfraConfig::forget('db.port');
class ServerConfig extends Confix
{
const string NAME = 'server';
const array DEFAULTS = ['port' => 80];
protected static array $_;
protected static function validate(string $name, mixed $value): bool
{
return match ($name) {
'port' => is_int($value) && $value > 0 && $value <= 65535,
default => true,
};
}
}
ServerConfig::set('port', 443); // ✅ true — stored
ServerConfig::set('port', -1); // ❌ false — rejected, old value kept
ServerConfig::set('port', 0, false); // bypasses validate(), stored as-is
AppConfig::get('debug'); // Can // Cannot
AppConfig::getDefault('version'); // '1.0.0'
AppConfig::has('name'); // Can // Cannot
AppConfig::hasDefault('version'); // true
AppConfig::hasDefault('ghost_key'); // false
AppConfig::set('debug', true); // true
AppConfig::set('port', -1); // false — validation failed
AppConfig::set('port', -1, false); // true — validation skipped
AppConfig::forget('debug');
AppConfig::get('debug'); // false — falls back to DEFAULTS
AppConfig::clear();
AppConfig::getData(); // []
$data = AppConfig::getData();
// ['name' => 'My Application', 'cache' => ['driver' => 'redis']]
$ref = &AppConfig::getDataByRef();
$ref['name'] = 'Updated';
AppConfig::get('name', false); // 'Updated'
AppConfig::setData(['name' => 'New App', 'debug' => false]);
AppConfig::setData(['name' => 'New App'], save: true); // writes file
AppConfig::load();
AppConfig::reload();
$results = AppConfig::reloadAllLoadedClasses();
// ['App\Config\AppConfig' => true, 'App\Config\CacheConfig' => true]
AppConfig::set('name', 'Updated');
AppConfig::save(); // writes config/app.php
AppConfig::remove();