Ending the PHP script with "Exit" would delete all variables and destroy objects?

Asked

Viewed 208 times

2

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

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

script completion would delete everything started or not started ?

how it makes a difference and how important it is to delete everything that was called or let’s say created (variables, array and objects)?

1 answer

4


YES.

Is in the manual: http://php.net/manual/en/function.exit.php
(for a change, in Portuguese 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 emphasis is more or less this:

Shutdown functions and object destructors are always executed, even if used Exit

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


Summary: use exit or its synonym die It is the same as the script ending "naturally". All resources are released (which is one of the reasons OOP in PHP is a waste of resources, each request has to recreate everything that is class again to be able to use).

Something else, unset it is not normal 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.

An example of valid use of unset is the one mentioned by Jorge Matheus in his comments, when it applies to a $_SESSION. This is because there it is no longer about memory, but data that is normally written to the disk, so that the next script recovers. Then it makes sense to clean up, because it is something that PHP will record at the end, and if it is something no longer desirable, there is no reason to preserve the information.

Still, it is worth the observation about OOP made before, because an object persisted in Session needs to be read from the file or DB, and de-serialized to become object again.

  • Bacco, referring to the quotation from unset, the same is suitable to "destroy" a session, for example?

  • 1

    @Jorgematheus just depends on your need. For safety’s sake instead of me destroying a session I’d rather empty it with $_SESSION = []; or even $_SESSION['objeto'] = null; for example, thus avoiding cookie reuse. In other cases you can reset a user ID on Session, repurposing it in case the user logs in and dislodges. Anyway, the UNSET in this case still makes some sense, because you are using to kill something that escapes from the script (the default Session is written to disk). No longer makes sense if it is 'free memory' in common variables

  • @Jorgematheus Summing Up, unset in Session fell into what I called "good reason" or "specific situation" ;)

  • Show Bacco! I asked the question because I use the function on logout of users.

  • 1

    great response as always. Thanks!

  • Before asking had searched in php but really the description does not match the English page you sent. Thanks =D

Show 1 more comment

Browser other questions tagged

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