PHP: destructor vs register_shutdown_function

后端 未结 4 1943
不思量自难忘°
不思量自难忘° 2020-12-16 01:19

I have a PHP class that creates a PNG image on the fly and sends it to browser. PHP manual says that I need to make sure that imagedestroy function is called at end

4条回答
  •  孤街浪徒
    2020-12-16 01:28

    I think one big thing that you have missed is that all the memory PHP has allocated during script execution is freed once the script terminates. Even if the user presses the stop-button, PHP processes the script until it is finished, gives it back to the HTTP daemon to be served to the visitor (or not, depending on how clever the daemon is).

    So, explicitly freeing up memory at the end of script execution is a bit redundant. Some might argue that it would be a good thing to do, but it's still redundant.

    But, on the topic of class destructors, they are called whenever the object is destroyed, either explicitly by unset() or at script completion/termination.

    The recommendation of the developer explicitly freeing up the memory used in image manipulation is sure just to make absolutely sure not to have a memory leak, as bitmaps can be straining on the memory side of things (height * width * bit depth * 3 (+ 1 if you have an alpha channel))

    To satisfy your Wikipedian needs:

    • http://php.net/manual/en/language.oop5.decon.php
    • http://php.net/manual/en/function.unset.php

提交回复
热议问题