Wait, What? Yeah that’s what I thought too. Still no Idea what I’m talking about? Well, let’s take a look at the following code. Let’s call it “fred”.
The code above, named “fred”, basically creates a static variable
$a inside the function
When you call
foo(0) it outputs the value of
$a. When you call
foo('x') , where x can be
anything, it updates the value of
'x', and outputs the new result.
Now, what would expect “fred” to output? If your like me, then you are completely wrong. “fred” will actually output the following code.
Yes, that’s what I said at the start, “Persistent Static Variables Across Instances”. The static
$a actually persists across the two instances of foo that “fred” created, and even into
the static method call. This was completely unexpected, at least by me.