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

issue.static_var

Edson Medina edited this page Jul 7, 2017 · 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.

Extract the resource and pass it as a parameter

Deal with the cache outside of the method.

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