Exiting PHP script with "exit" would delete all variables and destroy objects?

1

Hello, in PHP using the unset ($ variable) functions I delete the $ variable , in the $ Object-> destruct I destroy but still use unset ($ Object) .

My question is, if I use the exit function without deleting or destroying anything ...

  

Will script completion delete everything that was started or not?

     

How does this make a difference and how important is it to delete everything that was   call or let's say created (variables, array and objects)?

    
asked by anonymous 28.09.2018 / 18:09

1 answer

5

YES.

You are in the manual: link
(for a change, Portuguese are missing important pieces in the translation)

  

exit - Output a message and terminate the current script   Description

 void exit ([ string $status ] )
 void exit ( int $status )
     Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called .

The highlighted part is more or less this:

  

Shutdown functions and object destructors are always executed even if you use exit

("Even used exit", because this occurs at the end of script anyway)


Summary: use exit or its die synonymous gives the script a "naturally" ending. All the resources are released (which is one of the reasons OOP in PHP is a waste of resources, every request has to recreate everything that is new class to use).

Another thing, unset is not a normal thing to use in PHP. There has to be a very good reason for this, in very specific situations. Under normal conditions PHP, like most scripting languages, manages memory for you.

A valid use example of unset is the one mentioned by colleague Jorge Matheus in the comments, when it applies to a variable of $_SESSION . This is because it is no longer about memory, but about data that is normally written to disk, so that the next script will recover. It makes sense to clean, as it is something that PHP will write at closing, and if it is something not more desirable, you have no reason to preserve the information.

Still, it is worth observing about OOP made earlier, since an object persisted in session needs to be read from the file or DB, and de-serialized to become object again. >     

28.09.2018 / 18:20