Download & Extend

Avoid losing previous values on static variables

Project:Version Control API
Version:6.x-2.x-dev
Component:API module
Category:bug report
Priority:critical
Assigned:marvil07
Status:closed (fixed)
Issue tags:cache, static

Issue Summary

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

AttachmentSizeStatusTest resultOperations
0001-Avoid-losing-previous-values-on-static-variables.patch2.5 KBIgnored: Check issue status.NoneNone

Comments

#1

Version:5.x-2.0-rc2» 6.x-1.0-rc2

uppss.. bad version :-P

#2

Version:6.x-1.0-rc2» 6.x-2.x-dev
Status:needs review» fixed

ok, solved on HEAD since all changes in oop branch was included on HEAD

#3

Status:fixed» closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.