How .NET Handles PHP Static Local Variables

Moving on down the roadmap, we are implementing more and more PHP functionalities into Peachpie compiler. This week, we would like to introduce a particularly popular one – Static Local Variables.

Static Local Variables in PHP

The benefits of Peachpie compiler for PHP developers are relatively evident. However, we are frequently asked about some of the advantages that Peachpie compiler can bring to .NET developers, as well. PHP offers a number of unique features that, if used properly, have the ability to make your life a whole lot easier.

Static local variables are a good example of such a feature; when declared in a function scope, such a variable survives past the function’s end and retains its value until the next function invocation. PHP static locals are different to .NET statics, since PHP static variables only live in the frame of the current request, whereas in .NET, they live across all the requests and are not thread safe, for instance.

In fact, you will not find anything exactly equivalent to static locals in .NET, whilst this feature is commonly used and quite popular in PHP.

Compiling Statics From PHP to .NET

Compiling such a construct from PHP to .NET proved to be quite the challenge. Peachpie compiler generates efficient MSIL code that deals with statics, as can be seen in the following example.



private sealed class alreadyCalled<> {
public bool value = false;

public static void initializeMe(Context <ctx>) {
var alreadyCalled = <ctx>.GetStatic<alreadyCalled<>>();
if (alreadyCalled.value)

alreadyCalled.value = true;

Peachpie compiles the code into the C# equivalent above. It generates a class that holds the value of static variable. The logic of this construct is hidden in the implementation of Context, which is a special Peachpie object, representing a single web request. Internally, it remembers the instance of the holder class through generics. If you are interested in the details of this, you can find them here.


There are two basic and typical usages of static locals; counting something and checking to ensure that we do not enter into a function more than once:

Test Configuration:

  • Windows 10 x64
  • Lenovo Thinkpad Yoga 260
  • Core i7 6500U
  • 8GB DDR4 RAM


Again, PHP 7 has managed to tweak its performance significantly compared to PHP 5.6.

Peachpie handled the process even more quickly, however, requiring about 0.25 seconds, compared to the 1.68 seconds of PHP 7. PHP 5.6 needed 35.96 seconds, which we had trouble depicting properly in the diagram.

Posted on May 30, 2016, in category Benchmark, tags: , , , , , , , , , ,