Conf class - Ajaxel data holder
Conf class - Ajaxel data holder Posted by Alexander Shatalov I guess many people would as about this question....Enjoy PHP class Conf { private $vars = array(); private static $_instance = false; private static $seen = false; public function __construct($default = array()) { $this->vars = $default; } public static function &getInstance() { if (!self::$_instance) { self::$_instance = new self; } return self::$_instance; } public function __set($key, $val) { return $this->s($key, $val); } public function __get($key) { return $this->g($key); } public function __toString() { return p($this->vars,0); } public function __call($name, $args) { if (!self::$seen) p($this); self::$seen = true; } // key public function key($key) { return @key($this->vars[$key]); } // set public function s($key, $val='') { $this->vars[$key] = $val; } public function s2($key, $_key, $val='') { // if ($_key && is_array($_key)) $_key = 0; $this->vars[$key][$_key] = $val; } public function s3($key, $_key, $__key, $val='') { $this->vars[$key][$_key][$__key] = $val; } // collect public function c($key, $add=NULL) { if ($add) $this->vars[$key] .= $add; else $this->vars[$key] = ''; } public function c2($key, $_key, $add = '') { if ($add) $this->vars[$key][$_key] .= $add; else $this->vars[$key][$_key] = ''; } // plus public function plus($key,$num = 0) { if (!isset($this->vars[$key])) $this->vars[$key] = 0; $this->vars[$key] += $num; } public function plus2($key,$_key,$num = 0) { if (!is_array($this->vars[$key])) $this->vars[$key] = array(); $this->vars[$key][$_key] += $num; } public function plus3($key,$_key,$__key,$num = 0) { if (!isset($this->vars[$key]) || !is_array($this->vars[$key])) $this->vars[$key] = array(); if (!isset($this->vars[$key][$_key])) $this->vars[$key][$_key] = array(); if (!isset($this->vars[$key][$_key][$__key])) $this->vars[$key][$_key][$__key] = 0; $this->vars[$key][$_key][$__key] += $num; } // minus public function minus($key,$num = 0) { $this->vars[$key] -= $num; } public function minus2($key,$_key,$num = 0) { if (!is_array($this->vars[$key])) $this->vars[$key] = array(); $this->vars[$key][$_key] -= $num; } // array fill public function fill($key,$val) { if (!isset($this->vars[$key]) || !is_array($this->vars[$key])) $this->vars[$key] = array(); $this->vars[$key][] = $val; } public function unfill($key, $val) { unset($this->vars[$key][array_search($val,$this->vars[$key])]); } public function get() { $a = func_get_args(); switch (func_num_args()) { case 1: return @$this->vars[$a[0]]; break; case 2: return @$this->vars[$a[0]][$a[1]]; break; case 3: return @$this->vars[$a[0]][$a[1]][$a[2]]; break; case 4: return @$this->vars[$a[0]][$a[1]][$a[2]][$a[4]]; break; } } public function set() { $a = func_get_args(); switch (func_num_args()) { case 2: @$this->vars[$a[0]] = $a[1]; break; case 3: @$this->vars[$a[0]][$a[1]] = $a[2]; break; case 4: @$this->vars[$a[0]][$a[1]][$a[2]] = $a[3]; break; } return $this; } // get public function g($key,$default = false) { return (isset($this->vars[$key]) ? $this->vars[$key] : $default); } public function g2($key,$_key,$default = false) { if ($_key && is_array($_key)) $_key = 0; if (!isset($this->vars[$key]) || !is_array($this->vars[$key])) $this->vars[$key] = array(); return isset($this->vars[$key][$_key]) ? $this->vars[$key][$_key] : $default; } public function g3($key,$_key,$__key,$default = false) { if (!isset($this->vars[$key]) || !is_array($this->vars[$key])) $this->vars[$key] = array(); return isset($this->vars[$key][$_key][$__key]) ? $this->vars[$key][$_key][$__key] : $default; } public function g4($key,$_key,$__key,$___key,$default = false) { if (!isset($this->vars[$key]) || !is_array($this->vars[$key])) $this->vars[$key] = array(); return isset($this->vars[$key][$_key][$__key][$___key]) ? $this->vars[$key][$_key][$__key][$___key] : $default; } // is public function is($key) { if (isset($this->vars[$key])) return true; return false; } // exists public function exists($key, $_key) { if (isset($this->vars[$key]) && is_array($this->vars[$key])) return array_key_exists($_key, $this->vars[$key]); return false; } // in public function in($key, $_key) { if (isset($this->vars[$key]) && is_array($this->vars[$key])) return in_array($_key, $this->vars[$key]); return false; } // nulls public function n_array() { $args = func_get_args(); foreach ($args as $a) $this->vars[$a] = array(); } public function n_zero() { $args = func_get_args(); foreach ($args as $a) $this->vars[$a] = 0; } public function n_false() { $args = func_get_args(); foreach ($args as $a) $this->vars[$a] = false; } public function n_null() { $args = func_get_args(); foreach ($args as $a) $this->vars[$a] = NULL; } public function n_empty() { $args = func_get_args(); foreach ($args as $a) $this->vars[$a] = ''; } public function n_unset() { $args = func_get_args(); foreach ($args as $a) unset($this->vars[$a]); } // other public function merge($array = array()) { if (!is_array($array)) return false; $this->vars = array_merge((array)$this->vars,$array); } public function merge2($key, $array = array()) { if (!is_array($array)) return false; if (isset($this->vars[$key])) { $this->vars[$key] = array_merge((array)$this->vars[$key],$array); } else { $this->vars[$key] = $array; } } public function getAll() { return $this->vars; } } |