Call the function in PHP several times or store the function in variable?

Asked

Viewed 241 times

-2

I was here creating some loop files when I came across the following question:

Instead of calling a function that returns the die every time Ex:

<?php
if (pegar_dado('nome_dado') != '') {
    echo pegar_dado('nome_dado');
} ?>

It would not be more practical and maybe better and lighter store the variable and then do the rest?

<?php
$pega1 = pegar_dado('nome_dado');
if ($pega1 != '') {
 echo $pega1;
}

What I want to know is, from your experience, which of the two methods is better? Lighter? There is a difference in the use of memory in both cases?

And which of the two methods you recommend using?

  • Certainly the second option, imagine having to perform a function 1000 times... may be little change in the process, but it will occur, and once executed and saved in a variable, there is no need to run again.

  • Can you tell me if this "take data" function will be executed EVERY time I call this function? I mean, will she go through my database every time I call her? and if I store in a variable, will the database be consulted only once?

  • 3

    If the function makes a query to the bank, yes, the query will be made every time calling the function. Calling only once will be only a query made.

  • @Andersoncarloswoss If the function is called in the file several times it does the query, ok, but if the function is stored in variable and the variable is called several times, the query will be done 1 or several times?

  • 3

    Only once. The result will be stored in memory and reused.

  • 1

    It depends on the case. If the function is "pegIdSequencial" (and do what the name says) and you store the result, it will not be a sequential ID. Which to use? Depends on the intended outcome. In general, if you always want the current situation you have to call the function always, if you want the original value, even if something external changes the function result, store.

Show 1 more comment

1 answer

-3

The second way is not advisable.

There are caching strategies for cases involving IO. But the function should always be called.

In general, executing a function is more expensive than accessing a memory region with a value already assigned. But if it doesn’t involve IO, it won’t make much difference in the execution of your program.

However, you lose all the flexibility of your system. Imagine that in the future you need to make a decision to search for this data in different ways during the execution of the program. Or that you need to disable the cache of this feature in the future. Using the second approach, you would have to replace everywhere to remove this cache.

Whereas if you leave the responsibility of caching within the function, you would change only in one place.

Example cache inside function using OO:

class interfaceAcessoDado {

  private static $dadoCacheado;

  public function pegaDado()
  {
       if( self::$dataCache === null )
           self::$dadoCacheado = $this->pegaDadoDoBD();
       return self::$dadoCacheado;
  }

  private function pegaDadoDoBD()
  {
       // Busca esse dado 
       return $dado;
   }
}
  • 4

    I could not notice the difference/advantage of storing the value "cached" in a variable within the class with storing in the variable as asked that you said is not advisable. I could go into more detail?

  • I will point out two advantages. The first is that whoever is consuming this value does not need to know how it is being recovered. That is, if you need to use this function from several places, you do not need to keep repeating verification code if the variable exists other than BD search. It is always the same function that returns the value you want, regardless of how this is done. Doing this, it’s like you build an interface for data access.

  • @Andersoncarloswoss the second advantage is that if you have two totally different places that are accessing the same data, the first one performs the query and save this info in the static variable. If a second location of the code needs to access the same data, it will already consume the value of this variable. But all without having to wonder if you have already searched this data or not. This static variable exists at the execution time of a process. But if in the future you want something more sophisticated, you can leave in a redis for example and all the points consume this function nor "know" where the die is coming from.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.