Avoid losing previous values on static variables
marvil07 - July 10, 2009 - 21:17
| Project: | Version Control API |
| Version: | 6.x-1.0-rc2 |
| Component: | API module |
| Category: | bug report |
| Priority: | critical |
| Assigned: | marvil07 |
| Status: | needs review |
| Issue tags: | cache, static |
Jump to:
Description
If we initialize each time the variable, we lose previous changes, so for example cache variables are retrieved every call.
This patch tries to solve it.
Here is also a quick php test for structured and OO ;-)
<?php
function load() {
return array(1,2,3);
}
class a {
private static $cache;
public function get() {
if (!isset(self::$cache)) {
echo "cache __NOT__ set";
self::$cache = load();
}
else {
echo "cache set";
}
return self::$cache;
}
}
$obj = new a();
$arr = $obj->get();
$arr = $obj->get();
var_dump($arr);
echo "<hr>";
function sget() {
// without refering the variable with static again it's considered a new variable, not our static one
static $scache;
if (!isset($scache)) {
echo "cache __NOT__ set";
$scache = load();
}
else {
echo "cache set";
}
return $scache;
}
$arr2 = sget();
$arr2 = sget();
var_dump($arr2);
?>Also, already applied to my master branch if you want to pull :-D
| Attachment | Size |
|---|---|
| 0001-Avoid-losing-previous-values-on-static-variables.patch | 2.5 KB |

#1
uppss.. bad version :-P