Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 706
camrymps Avatar asked Dec 26 '15 01:12

camrymps


People also ask

Can a class be static in PHP?

In PHP, we can have both static as well as non-static (instantiated) classes. Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (method) or both.

When should I use static methods in PHP?

When to define static methods ? The static keyword is used in the context of variables and methods that are common to all the objects of the class. Therefore, any logic which can be shared among multiple instances of a class should be extracted and put inside the static method.

What is static property PHP?

Static properties ¶ Static properties are accessed using the Scope Resolution Operator ( :: ) and cannot be accessed through the object operator ( -> ). It's possible to reference the class using a variable. The variable's value cannot be a keyword (e.g. self , parent and static ).

What is self :: in PHP?

In PHP, you use the self keyword to access static properties and methods. The problem is that you can replace $this->method() with self::method() anywhere, regardless if method() is declared static or not.


1 Answers

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;
}
like image 96
Michael Berkowski Avatar answered Oct 21 '22 21:10

Michael Berkowski