PHP code example of vaibhavpandeyvpz / kunfig
1. Go to this page and download the library: Download vaibhavpandeyvpz/kunfig 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/ */
vaibhavpandeyvpz / kunfig example snippets
use Kunfig\Config;
// Create a configuration instance
$config = new Config([
'database' => [
'host' => 'localhost',
'port' => 3306,
'name' => 'myapp',
],
'app' => [
'name' => 'My Application',
'debug' => false,
],
]);
// Access values using object properties
echo $config->database->host; // 'localhost'
// Access values using array syntax
echo $config['app']['name']; // 'My Application'
// Access values using dot notation
echo $config->get('app.name'); // 'My Application'
echo $config['database.host']; // 'localhost'
// Access values using methods
echo $config->get('database')->get('port'); // 3306
use Kunfig\Config;
// Empty configuration
$config = new Config();
// With initial values
$config = new Config([
'key' => 'value',
'nested' => [
'deep' => 'value',
],
]);
// Method access
$value = $config->get('key');
$value = $config->get('nonexistent', 'default'); // with fallback
// Property access
$value = $config->key;
$nested = $config->nested->deep;
// Array access
$value = $config['key'];
$nested = $config['nested']['deep'];
// Dot notation (alternative to nested access)
$nested = $config->get('nested.deep');
$nested = $config['nested.deep'];
$value = $config->get('app.database.host', 'localhost'); // with fallback
// Method access
$config->set('key', 'value');
$config->set('nested', ['deep' => 'value']);
// Property access
$config->key = 'value';
$config->nested = new Config(['deep' => 'value']);
// Array access
$config['key'] = 'value';
$config['nested'] = ['deep' => 'value'];
// Dot notation (creates nested structure automatically)
$config->set('app.debug', true);
$config['database.host'] = 'localhost';
$config->set('app.database.port', 3306); // creates nested structure
// Arrays are automatically converted to Config instances
$config->set('database', ['host' => 'localhost']);
$config->database->host; // 'localhost' (automatically a Config instance)
// Method access
if ($config->has('key')) {
// key exists
}
// Property access
if (isset($config->key)) {
// key exists
}
// Array access
if (isset($config['key'])) {
// key exists
}
// Dot notation
if ($config->has('app.debug')) {
// nested key exists
}
if (isset($config['app.debug'])) {
// nested key exists
}
// Method access
$config->remove('key');
// Property access
unset($config->key);
// Array access
unset($config['key']);
// Dot notation
$config->remove('app.debug');
unset($config['database.host']);
$config = new Config([
'app' => [
'name' => 'MyApp',
'debug' => false,
'database' => [
'host' => 'localhost',
'port' => 3306,
],
],
]);
// Instead of: $config->app->database->host
$host = $config->get('app.database.host'); // 'localhost'
// Instead of: $config['app']['database']['port']
$port = $config['app.database.port']; // 3306
// With fallback
$timeout = $config->get('app.database.timeout', 30); // 30 (default)
$config = new Config();
// Creates nested structure automatically
$config->set('app.debug', true);
$config['app.database.host'] = 'localhost';
$config->set('app.database.port', 3306);
// Now accessible via all methods
$config->app->debug; // true
$config['app']['database']['host']; // 'localhost'
$config->get('app.database.port'); // 3306
// Check nested keys
if ($config->has('app.debug')) {
// app.debug exists
}
if (isset($config['app.database.host'])) {
// app.database.host exists
}
// Remove nested keys
$config->remove('app.debug');
unset($config['app.database.port']);
// Remove entire nested branches
$config->remove('app.database'); // removes entire database config
$config = new Config([
'level1' => [
'level2' => [
'level3' => [
'level4' => 'deep_value',
],
],
],
]);
// Access deeply nested values
$value = $config->get('level1.level2.level3.level4'); // 'deep_value'
$config->set('level1.level2.level3.level4.new', 'value');
$config = new Config([
'app' => 'simple_string', // non-config value
]);
// Accessing nested key on non-config value returns fallback
$value = $config->get('app.key', 'default'); // 'default'
// Setting nested key on non-config value converts it to Config
$config->set('app.key', 'value');
$config->get('app'); // Returns ConfigInterface instance
$config->get('app.key'); // 'value'
$base = new Config([
'database' => [
'host' => 'localhost',
'port' => 3306,
'name' => 'production',
],
'app' => [
'name' => 'My App',
'debug' => false,
],
]);
$override = new Config([
'database' => [
'port' => 5432, // Override port
'name' => 'staging', // Override name
// host remains 'localhost'
],
'app' => [
'debug' => true, // Override debug
// name remains 'My App'
],
'cache' => [
'enabled' => true, // New key
],
]);
$base->mix($override);
// Result:
// $base->database->host === 'localhost' (unchanged)
// $base->database->port === 5432 (overridden)
// $base->database->name === 'staging' (overridden)
// $base->app->name === 'My App' (unchanged)
// $base->app->debug === true (overridden)
// $base->cache->enabled === true (new)
$config = new Config([
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
]);
foreach ($config as $key => $value) {
echo "$key: $value\n";
}
$config = new Config([
'app' => [
'name' => 'My App',
'version' => '1.0.0',
],
'debug' => false,
]);
$array = $config->all();
// Returns:
// [
// 'app' => [
// 'name' => 'My App',
// 'version' => '1.0.0',
// ],
// 'debug' => false,
// ]
$config = new Config([
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
]);
echo count($config); // 3
use Kunfig\Config;
// Load base configuration
$config = new Config([
'database' => [
'host' => 'localhost',
'port' => 3306,
'charset' => 'utf8mb4',
],
'cache' => [
'driver' => 'file',
'ttl' => 3600,
],
]);
// Load environment-specific overrides
$envConfig = new Config([
'database' => [
'host' => getenv('DB_HOST') ?: 'localhost',
'name' => getenv('DB_NAME') ?: 'myapp',
'user' => getenv('DB_USER') ?: 'root',
'password' => getenv('DB_PASSWORD') ?: '',
],
'cache' => [
'ttl' => 7200, // Override TTL
],
]);
// Merge configurations
$config->mix($envConfig);
// Use the configuration (multiple access patterns)
$pdo = new PDO(
sprintf(
"mysql:host=%s;port=%d;dbname=%s;charset=%s",
$config->database->host, // Property access
$config->get('database.port'), // Dot notation
$config['database']['name'], // Array access
$config->get('database')->get('charset') // Method chaining
),
$config->get('database.user'), // Dot notation
$config->database->password // Property access
);
public function __construct(array $values = [])
public static function __set_state(array $data): static
use Kunfig\ConfigInterface;
use Kunfig\ConfigTrait;
class MyCustomConfig implements ConfigInterface
{
use ConfigTrait;
protected array $values = [];
// Implement
$config = new Config([
'port' => 3306,
]);
// Type-safe access
$port = $config->get('port', 0); // int
$host = $config->get('host', 'localhost'); // string
$config = new Config([
'string' => 'text',
'integer' => 42,
'float' => 3.14,
'boolean' => true,
'null' => null,
'array' => ['nested' => 'value'],
]);
// All types are preserved
$config->string; // string
$config->integer; // int
$config->float; // float
$config->boolean; // bool
$config->null; // null
$config->array; // ConfigInterface instance