Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

issue.static_var

edsonmedina edited this page Dec 16, 2014 · 3 revisions

#Static variable

function foo() 
{
    static $cache;
 
    //...
}

##Why is this a testing issue?

While useful sometimes (ie: caching or singletons), the value of static variables can't be controlled from outside the method, and thus it's unpredictable. If it's unpredictable, it's untestable.

##Possible refactorigns

####Make it non static

And lose the caching feature.

####Externalize the resource

function foo($cache) 
{
    // ...
  
    $cache = 'bar';
    return $cache;
}

Clone this wiki locally