Are the variables of a function automatically deleted after its termination?

Asked

Viewed 85 times

3

I am developing a function that has variables that carry a lot of data and as these variables are used only within this function it does not make sense that they continue occupying space in memory, at first I carried out the exclusion of them using the function unset(), but I came to doubt: "After a function finishes the variables of the same are erased automatically as at the end of the script as a whole, or the use of the unset() is really necessary?"

The function follows more or less the following structure:

<?php
    function metodox($x,$y,$z)
    {
       //Nesse ponto a variável return recebe o resultado de uma requisição a uma API retornado pelo metodoy
       $return = $metodoy($url);
       $dados = json_decode($return);
     //Operações sobre o conteúdo da variável $dados

       //Ao fim realizo os issets
       isset($return);
       isset($dados);
     }

I did not put the original function because it is very extensive, however, the basic structure is this.

3 answers

4

The question is a little confused because it speaks in method and uses an example that seems to be a function (it could be just an excerpt from a class).

The local variables of a function or method are no longer visible and go out of scope when the execution of the function is complete. Possibly the life time ends as well, but this depends on some factors such as the existence of a reference to an object pointed by that variable.

Variables that are not local to the function or method continue to exist normally.

You don’t need to try to remove the local variables, and this doesn’t even make much sense.

Parameters are considered local variables.

Almost always wearing one unset() is doing something wrong, no matter what variable. Almost every case is gambiarra to circumvent a problem and the right one would solve the problem.

  • Actually, if it is a function, I will correct the error in the question. I don’t usually use the unset() because from what I have seen, including in your answer in a question about the use of it, the use of it can lead to a greater memory expenditure for its execution.

3


Variables within functions are automatically deleted at completion, in short it is only alive while the function is running.

Reference:

https://stackoverflow.com/questions/14082870/does-php-free-local-variables-immediately-after-the-function-ends

However if it is outside a function recommend the use of the function unset();.

A tip you can do is to work with vectors, for example: $array['return']; and $array['dados']; at the end use only one unset($array); with this would eliminate everything in just one variable.

  • I always end up changing the names unset() for isset(), I already corrected the error in the question. My doubt in reality is what happens to variables of a method after its termination, they are automatically deleted?

  • @Thiagodrulla, yes I saw that corrected added what I wanted to know at the end of the reply. (but it is one of the options that can do)

  • If I don’t perform the unset() at the end of the method, variables will continue to occupy memory space until the end of the script?

  • 1

    If you go inside a function it will be deleted automatically, (face now that it is inside a function); it will only be alive while the function is running, after the end of it will be cleared the variable.

  • exactly what I wanted to know, put this your last explanation in the answer if possible

  • 1

    @Thiagodrulla changed the answer, and added a reference from gringo stack. a read in the question comments.

Show 1 more comment

1

The behavior of unset() may vary within a function depending on the type of variable you are trying to destroy.

If you use unset() in a global variable within a function, only the local variable will be destroyed. The variable in the environment that was called will have the same value as before the execution of unset().

<?php
function destroi_teste()
{
    global $teste;
    unset($teste);
}

$teste= 'bar';
destroi_teste();
echo $teste;
?>

The above example will print:

bar

Source: http://php.net/manual/en/function.unset.php

Browser other questions tagged

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