Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@laverboy
Created June 8, 2015 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save laverboy/db9e2a281b50baa3441c to your computer and use it in GitHub Desktop.
Save laverboy/db9e2a281b50baa3441c to your computer and use it in GitHub Desktop.
Dot Notation Accessor
<?php
return [
'version' => '1.0.0',
'table' => 'table_name',
'info' => [
'item' => [
'name' => 'cherry',
'value' => 'pick'
]
]
];
<?php
function config($name)
{
$config = require __DIR__ . '/config.php';
$pieces = explode('.', $name);
foreach ($pieces as $piece) {
if (!is_array($config) || !array_key_exists($piece, $config)) {
// error occurred
throw new \InvalidArgumentException('That config key does not exist.');
}
$config = &$config[$piece];
}
return $config;
}
// Use
$version = config('version');
$value = config('info.item.value');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment