camrymps
camrymps

Reputation: 327

Storing data in a static class [PHP]

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;
}

Upvotes: 6

Views: 1968

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270637

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;
}

Upvotes: 4

Related Questions