Static variable for optimization
Posted
by keithjgrant
on Stack Overflow
See other posts from Stack Overflow
or by keithjgrant
Published on 2010-03-18T21:49:52Z
Indexed on
2010/03/18
22:11 UTC
Read the original article
Hit count: 192
I'm wondering if I can use a static variable for optimization:
public function Bar() {
static $i = moderatelyExpensiveFunctionCall();
if ($i) {
return something();
} else {
return somethingElse();
}
}
I know that once $i
is initialized, it won't be changed by by that line of code on successive calls to Bar()
. I assume this means that moderatelyExpensiveFunctionCall()
won't be evaluated every time I call, but I'd like to know for certain.
Once PHP sees a static variable that has been initialized, does it skip over that line of code? In other words, is this going to optimize my execution time if I make a lot of calls to Bar()
, or am I wasting my time?
© Stack Overflow or respective owner