Wäre das Ihre Bedürfnisse?
Ich hatte so etwas wie dies auf diese Weise umgesetzt:
<?php
class Settings {
// [...] Some stuff
/**
* Sets a value to the corresponding key
* @throws SettingsException If $key is not well written
* @param string $key The depth slahes key
* @param mixed The value to set
*/
public function setValue($key, $value){
if(strpos($key, '/') !== false){
if(($key = trim(preg_replace('#[/]+#', '/', $key), ' /')) === ''){
throw new SettingsException('"' . $key . '" is not valid');
}
$this->_setValue($key, $this->_currentGroup, $value);
} else {
$this->_currentGroup[$key] = $value;
}
$this->_saved = false;
return $this;
}
/**
* Return a setting value
* @throws SettingsException If the key doesn't exists or is null
* @param string $key The depth slashes key
* @return mixed The value corresponding to the key
*/
public function value($key){
if(strpos($key, '/')){
if(($key = trim(preg_replace('#[/]+#', '/', $key), ' /')) === ''){
throw new SettingsException('"' . $key . '" is not valid');
}
return $this->_value($key, $this->_currentGroup, $key);
} else if(isset($this->_currentGroup[$key])) {
return $this->_currentGroup[$key];
} else {
throw new SettingsException('"' . $key . '" is not a valid key');
}
}
private function _setValue($key, &$configDepth, $value){
if(($pos = strpos($key, '/'))){
if(!isset($configDepth[($k = substr($key, 0, $pos))])) {
$configDepth[$k] = array();
}
$this->_setValue(substr($key, $pos+1), $configDepth[$k], $value);
} else {
$configDepth[$key] = $value;
}
}
private function _value($key, &$configDepth, $prefix){
if(($pos = strpos($key, '/'))){
if(!isset($configDepth[($k = substr($key, 0, $pos))])) {
throw new SettingsException('The prefix "' . $prefix . '" doesn\'t exists');
}
return $this->_value(substr($key, $pos+1), $configDepth[$k], $prefix);
} else if(!isset($configDepth[$key])) {
throw new SettingsException('The prefix "' . $prefix . '" doesn\'t exists');
} else {
return $configDepth[$key];
}
}
}
Einstellungen und immer Wert funktioniert wie die oben:
<?php
$s = new Settings;
$s->setValue('main/foo', 'bar'); // $s::_s[main][foo] = bar;
$main = $s->value('main'); // return Setting like an array(foo => bar);
$bar = $s->value('main/foo'); // return string value bar
//$s['main'] == $s->value('main');
// $s['main']['foo'] = 'bar' == $s->setValue('main/foo', 'bar');
Sie nicht brauchen set_value ($ name, $ value) noch get_value ($ Namen), denke über Testen und Debuggen nach. Verwenden Sie Setter/Getter, wo Sie sie brauchen. –