i show u my example (memory allocator code) base on your example
this code first posted in
http://www.phpbuilder.com/board/showthread.php?s=&threadid=10245354
function memory() {
static $data;
switch(func_get_arg(0)) {
case 'a':
$args = func_get_args();
memory_parse($args[1]);
@eval("\$data{$args[1]} = \$args[2];");
break;
case 'g':
$args = func_get_args();
memory_parse($args[1]);
@eval("\$r = \$data$args[1];");
return isset($r) ? $r : false;
break;
case 'dbg':
return $data;
break;
}
}
function a($oMap, $oData) {
return memory('a', $oMap, $oData);
}
function g($oMap) {
return memory('g', $oMap);
}
function memory_parse(&$oMap) {
if(substr_count($oMap, '.') != 0) {
$oMap = explode('.', $oMap);
$oMap = "['".implode("']['", $oMap)."']";
if(substr($oMap, -4) == "['']") {
$oMap = substr($oMap, 0, -4).'[]';
}
} else {
$oMap = "['".$oMap."']";
}
}
// to add a new style
a('styleA.link.color', strtolower($_SESSION['alink']));
a('styleA.visited.color', strtolower($_SESSION['vlink']));
a('styleB.linkA.color', strtolower($_SESSION['alink']));
a('styleB.linkB.color', strtolower($_SESSION['alink']));
a('styleB.visited.color', strtolower($_SESSION['vlink']));
a('styleC.link.color', strtolower($_SESSION['alink']));
a('styleC.visited.color', strtolower($_SESSION['vlink']));
a('styleD.link.color', strtolower($_SESSION['alink']));
a('styleD.visited.color', strtolower($_SESSION['vlink']));
// if u don't like to set A B C D, u can just 2 dots.
a('style..link.color', strtolower($_SESSION['alink']));
// u can have as many styles u want
// the style will be returned in array form when u
g('styleA');
g('styleB.link');
// and so on...
// to debug
$a = memory('dbg');
print_r($a);
// will show all the variables values
i am not OOP type, me procedural type... 🙂 i don't really like "->" to access, setting variables.
this is part of my implementation to manageble code.
first, it simplify the making and getting of array and prevent the use of class OOP to store variables' values.
all my functions are stored in a module way where they all are intergrated with this core function memory.
upon application initialization, all configuration data will be stored into the memory. (i use fread() on WDDX file)
$handler = fopen('config.xml', 'rb');
$data = fread($handler, filesizeof('config.xml'));
fclose($handler);
a('config', $data);
// then i can access the configuration data anywhere, anyscope.
em... i generally didn't comment my code. 🙁 but i think it is easy to understand lah... u can try it and comment back to me so i can see what other's expectation and try to improve it.
basically, this is one of the good code i ever wrote.. 🙂
i got another script which implement building array
// this is cumbersome
$a = array('apple', 'orange', 'pinapple');