'Is there an easier way to do this with static variables?
I have some old code where I took functions like this:
function getDataPlain() {
$theArray = fetchFromDb("select * from tablename");
return $theArray;
}
and converted them to use static like this:
function getDataStaticVersion() {
static $theArray;
if (isset($theArray)) {
return $theArray;
}
else {
$theArray = fetchFromDb("select * from tablename");
return $theArray;
}
}
Then, if the method was called more than once it would only hit the database once. This works well but I'm wondering if there's a cleaner way to do this with less code that has to be written in each function. (I have a number of functions like this that I'd like to convert to the static version)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
