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 ;-)


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

Comments

marvil07’s picture

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

uppss.. bad version :-P

marvil07’s picture

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

Status: Fixed » Closed (fixed)
Issue tags: -cache, -static

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