PHP code example of phlak / config

1. Go to this page and download the library: Download phlak/config 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/ */

    

phlak / config example snippets


use PHLAK\Config\Config;

$config = new Config($context, $prefix = null);

  
  
  return [
      'driver' => 'mysql',
      'drivers' => [
          'sqlite' => [
              'database' => 'database.sqlite',
              'prefix' => ''
          ],
          'mysql' => [
              'host' => 'localhost',
              'database' => 'blog',
              'username' => 'blogger',
              'password' => 'hunter2',
              'charset' => 'utf8',
              'prefix' => ''
          ]
      ]
  ];
  

Config::__construct( mixed $context [, string $prefix = null ] ) : Config

$config = new Config('path/to/conifg.yaml');

$config = new Config('path/to/conifgs/');

$config = new Config([
    'hostname' => 'localhost',
    'port' => 12345
]);

Config::set( string $key, mixed $value ) : bool

$config->set('hostname', 'localhost');
$config->set('port', 12345);

Config::get( string $key [, mixed $default = null ] ) : mixed

// Return the hostname option value or null if not found.
$config->get('hostname');

// Returns 'localhost' if hostname option is not set
$config->get('hostname', 'localhost');

Config::has( string $key ) : bool

$config = new Config([
    'hostname' => 'localhost'
]);

$config->has('hostname'); // Returns true
$config->has('port');     // Returns false

Config::append( string $key, mixed $value ) : bool

$config->set('tags', ['foo', 'bar'])
$config->append('tags', 'baz'); // ['foo', 'bar', 'baz']

Config::append( string $key, mixed $value ) : bool

$config->set('tags', ['foo', 'bar'])
$config->append('tags', 'baz'); // ['baz', 'foo', 'bar']

Config::unset( string $key ) : bool

$config->unset('hostname');

Config::load( string $path [, string $prefix = null [, bool $override = true ]] ) : self

$conifg->load('path/to/config.php');

$config->load('databaes.php', 'database');

$config->load('additional-options.php', null, false);

Config::merge( Config $config [, bool $override = true ] ) : self

$anotherConfig = new Config('some/config.php');

$config->merge($anotherConfig);

$anotherConfig = new Config('some/config.php');

$config->merge($anotherConfig, false);

Config::split( string $key ) : Config

$config = new Config([
    'foo' => 'foo',
    'bar' => [
        'baz' => 'barbaz'
    ],
]);

$barConfig = $config->split('bar');

$barConfig->get('baz');  // Returns 'barbaz'

Config::toArray( void ) : array

$config = new Config(['foo' => 'foo']);

$config->toArray(); // Returns ['foo' => 'foo']