0

I created a class to store config values.

The code of my Config class:

class Config
{
protected static $params = array();
protected static $instance;

private function __construct(array $params=NULL){
    if(!empty($params)){
        self::$params = $params;
    }
}

public static function init(array $params=NULL){
    if(!isset(self::$instance))
        self::$instance = new Config($params);
}

public static function getInstance(){
    return self::$instance;
}

public static function initialized(){
    return isset(self::$instance);
}


public static function get($name)
{
    $ref = &self::$params;
    $keys = explode('.', $name);
    foreach ($keys as $idx => $key):
        if (!is_array($ref) || !array_key_exists($key, $ref))
            return NULL;
        $ref = &$ref[$key];
    endforeach;
    return $ref;        
}

public function delete($name){
    $ref = &self::$params;
    $keys = explode('.', $name);
    foreach ($keys as $idx => $key):
        if (!is_array($ref) || !array_key_exists($key, $ref))
            return NULL;
        $ref = &$ref[$key];
    endforeach;

    unset($ref);        
}

public function set($name, $value) {

    $ref = &self::$params;
    $keys = explode('.', $name);
    foreach ($keys as $idx => $key) {
        if (!is_array($ref)) {
            return false;
            throw new Exception('key "'.implode('.', array_slice($keys, 0, $idx)).'" is not an array but '.gettype($ref));
        }
        if (!array_key_exists($key, $ref)) {
            $ref[$key] = array();
        }
        $ref = &$ref[$key];
       }


    $ref = $value;
    return true;
}

public function getAll(){
    return self::$params;
}

public function clear(){
    self::$params = array();
}

}

Config methods uses a dot format:

Config::set("test.item","testdrive"); //store in class, array["test"]["item"]="testdrive"

But, im trying to create a method to remove values, for example:

Config::delete("test.item"); 

In get and set methods, i use reference variables to find the correct item, but I don't know how i can remove a referenced variable. If i use unset($ref), Config::$params is unaffected.

Brad
  • 159,648
  • 54
  • 349
  • 530
Stefan Luv
  • 1,179
  • 3
  • 12
  • 28
  • 1
    not sure why you are using references so much here. – J_B Mar 24 '13 at 17:21
  • if u use for example Config::get("website.forum.postsperpage") the class needs to got dinamically self::$params["website"]["forum"]["postperpage"]. Its the only way(i think) to manage dinamically nested arrays. – Stefan Luv Mar 24 '13 at 17:25

1 Answers1

0

when you do $ref = &$ref[$key]; then $ref will hold the value (not ref) of $ref[$key], so better apply unset() over $ref[$key] which will unset its reference, like this:

public function delete($name){
    $ref = &self::$params;
    $keys = explode('.', $name);
    foreach ($keys as $idx => $key):
        if (!is_array($ref) || !array_key_exists($key, $ref))
            return NULL;
    endforeach;

    unset($ref[$key]);        
}
Nelson
  • 49,283
  • 8
  • 68
  • 81