6

Hi everyone and Merry Christmas!

I am having some trouble with efficiency and I am hoping the StackOverflow community can help me.

In one of my (static) classes, I have a function that takes a large amount of information from my database, parses that information and puts it in a formatted array. Many functions within this class rely on that formatted array and throughout the class, I call it several times, which means that the application goes through this processes several times in a single run, which I am assuming is not very efficient. So I am wondering if there is a more efficient way I can go about doing this. Is there a way for me to store the formatted array within the static function so that I do not have to re-do the entire process every time I need information from the formatted array?

private static function makeArray(){ 
   // grab information from database and format array here
   return $array;
}

public static function doSomething(){
   $data = self::makeArray();
   return $data->stuff;
}

public static function doSomethingElse(){
   $data = self::makeArray();
   return $data->stuff->moreStuff;
}
camrymps
  • 327
  • 3
  • 11
  • Is the result of `makeArray()` always the same on every call? If so, you may cache it in a static property and return that if already populated. – Michael Berkowski Dec 26 '15 at 03:09
  • If the data in the database is updated (which does not happen that often and is done manually) then it will change the result of makeArray() so caching it may be a good option. How would you suggest I go about caching such a result? – camrymps Dec 26 '15 at 03:11

1 Answers1

4

If the result of makeArray() is not expected to change during one run of your script, consider caching the result of it in a static class property after the first time it is retrieved. To accomplish this, check if the variable is empty. If it is, perform the database action and save the result. If non-empty, just return the existing array.

// A static property to hold the array
private static $array;

private static function makeArray() { 
   // Only if still empty, populate the array
   if (empty(self::$array)) {
     // grab information from database and format array here
     self::$array = array(...);
   }
   // Return it - maybe newly populated, maybe cached
   return self::$array;
}

You may even add a boolean parameter to the function which forces a fresh copy of the array.

// Add a boolean param (default false) to force fresh data
private static function makeArray($fresh = false) { 
   // If still empty OR the $fresh param is true, get new data
   if (empty(self::$array) || $fresh) {
     // grab information from database and format array here
     self::$array = array(...);
   }
   // Return it - maybe newly populated, maybe cached
   return self::$array;
}

All your other class methods may continue calling self::makeArray() as you already have done.

public static function doSomething(){
   $data = self::makeArray();
   return $data->stuff;
}

If you added the optional fresh parameter and want to force a retrieval from the database

public static function doSomething(){
   // Call normally (accepting cached values if present)
   $data = self::makeArray();
   return $data->stuff;
}
public static function doSomethingRequiringRefresh(){
   // Call with the $fresh param true
   $data = self::makeArray(true);
   return $data->stuff;
}
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390