Skip to content

Instantly share code, notes, and snippets.

@treffynnon
Created September 3, 2010 09:20
Show Gist options
  • Save treffynnon/563670 to your computer and use it in GitHub Desktop.
Save treffynnon/563670 to your computer and use it in GitHub Desktop.
A PHP class to access a PHP array via dot notation
<?php
namespace Treffynnon;
/**
* A PHP class to access a PHP array via dot notation
* (Agavi http://www.agavi.org was the inspiration).
*
* This was hacked in to an existing codebase hence the
* global config array variable.
*
* The global $config variable should be an associative array like:
* $config = array(
* 'database' => array(
* 'host' => 'localhost',
* 'user' => 'Treffynnon',
* 'pass' => 'password',
* 'db' => 'database',
* ),
* );
*
* But it can be as deeply nested as you like. An example of how
* to access the values for the example config array above is
* given below.
*
* @example $host = Config::get('database.host');
* @author Simon Holywell
*/
class Config {
/**
* Get a particular value back from the config array
* @global array $config The config array defined in the config files
* @param string $index The index to fetch in dot notation
* @param string $default If the requested index/value does not exist then the default value (as long as it is not null) will be returned
* @return mixed
*/
public static function get($index, $default = null) {
global $config;
$index = explode('.', $index);
try {
$return = self::getValue($index, $config);
} catch (ConfigException $e) {
if(!is_null($default)) {
$return = $default;
} else {
throw $e;
}
}
return $return;
}
/**
* Navigate through a config array looking for a particular index
* @param array $index The index sequence we are navigating down
* @param array $value The portion of the config array to process
* @return mixed
*/
protected static function getValue($index, $value) {
if(is_array($index) and
count($index)) {
$current_index = array_shift($index);
}
if(is_array($index) and
count($index) and
isset($value[$current_index]) and
is_array($value[$current_index]) and
count($value[$current_index])) {
return self::getValue($index, $value[$current_index]);
} elseif(isset($value[$current_index])) {
return $value[$current_index];
} else {
throw new ConfigException("Attempt to access missing configuration variable: $current_index");
}
}
}
class ConfigException extends Exception {}
<?php
require_once 'classes/Config.php';
// this is a completely contrived example
$config = array(
'site' => array(
'title' => 'Treffynnon',
'description' => 'an exciting package',
'keywords' => 'exciting, demo, example, package',
'author' => array(
'name' => array(
'title' => 'Mr.',
'first' => 'Simon',
'second' => 'Holywell'
),
'email' => '[email protected]',
),
),
'database' => array(
'host' => 'localhost',
'database' => 'my_database_name',
'username' => 'my_database_username',
'password' => 'my_database_password',
),
'version' => '0.0.2',
);
$title = Config::get('site.author.name.title');
$second_name = Config::get('site.author.name.second');
$last_name = Config::get('site.author.name.last', 'Holywell'); // Example of setting a default for non-existent values
$email = Config::get('site.author.email');
?>
<p>To email <?php echo $title; ?> <?php echo $second_name; ?> <?php echo $last_name; ?> please click
<a href="mailto:<?php echo $email; ?>">here</a>.</p>
@Jeff-Russ
Copy link

Jeff-Russ commented Dec 30, 2016

I just made a super ambitious version of this, as a array object class I call 'Tree' but it turned out to have massive memory leak issues. I now going to rewrite it with a more simple approach like yours!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment