Reputation: 11545
class A{
static $var = true;
function f(){
}
}
vs
class A{
function f(){
static $var = true;
}
}
There doesn't seem to be any difference. Are there any advantages of using one over the other?
Note that $var
would be used in the f()
function only. I understand that declaring it in the class header is required if the variable needs to be used in multiple functions
Upvotes: 1
Views: 65
Reputation: 505
In the later example you may only use the var inside the f
function. The other is accessible anywhere from inside the class A::var
or outside A::var
Upvotes: 1
Reputation: 16351
If you only use the static variable in the f
function, there's only a scope difference, that means no difference as long as you don't try to use it elsewhere.
When used in local scope, the static variable value is kept between each function call. See Static variables part of this page.
Thanks insertusernamehere for pointing that out.
Upvotes: 3