Scenario:
- there is a global array
$cache. - there is 1 function,
cacheWriter(), that updates$cachewith
various cachable objects.cacheWriter()runs a switch() with
different cases that each update a certain key in$cache. - some cases in
cacheWriter()depend on other cases to correctly
update$cache. In these cases, the script checks if the array key
it depends on already exists in$cache, and if not, it will call
cacheWriter()from within to get the case it needs.
In these cases, will $cache already be updated and contain the new content, or only the next time the function runs?
Example:
function cacheWriter($case) {
global $cache;
if($cache[$case]) {
$out = $cache[$case];
} else {
switch($case) {
case 1 :
$cache[1] = 'some object';
break;
case 2 :
if(!$cache[1]) {
$dummy = cacheWriter(1);
}
//QUESTION:
//will $cache[1] now exist right here (since it's global)
//so that I can now simply access it like this:
$cache[2] = $cache[1]->xyz;
//OR,
//do I have to use $dummy to get [1]
//and $cache[1] will only exist the next time the function runs?
break;
}
$out = $cache[$case];
}
return $out;
}//cacheWriter()
Obviously, this function is extremely simplified, but it’s the basic concept. Thanks!
Yes, the value of the global will contain the last write. The
globaldirective is for scoping purposes, to tell the interpreter which variable to use – it doesn’t actually “import” the value and hold onto it at that point.