Created
June 8, 2015 08:04
-
-
Save laverboy/db9e2a281b50baa3441c to your computer and use it in GitHub Desktop.
Dot Notation Accessor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'version' => '1.0.0', | |
'table' => 'table_name', | |
'info' => [ | |
'item' => [ | |
'name' => 'cherry', | |
'value' => 'pick' | |
] | |
] | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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