When looking for references to good practices in PHP
for memory management, I came across a number of ways to use destructors .
I know the main actor is the Garbage Collector but this one is not Garbage Collector is part of the scope of my question.
References:
- PHP Manual
- among some of the community highlight this
- and a external reference
But my readings bring me doubts which I did not find answers being them ...
Questions:
- will the empty co_de function remove any value associated with the class or do you need to explicitly state these values in your scope?
-
After instantiating the class by reference and before the script closes the difference between using
__destruct(){}
andNULL
for example:<?php $refer = new MyClass(); $refer->hello(); // output: Hello World! $refer = NULL; // ou unset($refer); // mais blocos de código aqui...
-
According to PHP Manual class inheritance requires the "daughter" class to explicitly call
unset()
in destructor ... bad and in case of non-extended classes that instantiate another class in its scope? When assigningparent::__destruct()
or usingNULL
before the script closes will you force both destructors or need to be explicit? Example:<?php /** class Core { public function __construct(){} public function session($start=true) { if ( !$start ) { if ( isset($_SESSION) ) { session_destroy(); return true; } } session_start(); } public function language() { if ( isset($_SESSION) ) { $_SESSION['language'] = 'pt-BR'; } } public function layoutEngine() { return new HandlerUI(); // outra classe } function __destruct(){ # } } class HandlerUI { function __construct() { return $this; } public function Header() { return "<head><title>Hello World</title></head>"; } public function NavBar() { return "<nav>This is navbar</nav>"; } public function Drawer() { return "<div class='drawer-container'></div>"; } public function Footer() { return "<footer>I am Footer</footer?"; } public function JavaScript() { return "<script type='text/javascript'>console.log('hola que tal');</script>"; } function __destruct(){ # } } */ // caso de uso: $app = new Core() $app->session(); $app->language(); $layout = $app->layoutEngine(); unset($app); // ou $app = NULL; ?> <!DOCTYPE html> <html lang="<?php echo $_SESSION['language'];?>"> <?php $layout->Header(); ?> <body id="body"> <!-- CONTAINER --> <section class="default"> <?php // navbar $layout->Navbar(); // drawer $layout->Drawer(); ?> <!-- CENTRAL BLOCK --> <section id="central-block"></section> <?php // $layout->Footer(); ?> </section> <?php // $layout->JavaScript(); unset($layout); // ou $layout = NULL; // mais blocos de código aqui... ?> </body> </html>
- and I wonder more : using OPcache or another bytecode cache (which stores in compiled memory) how the
unset()
fuction is handled in this cache usage case?
I know that there is no "right way" since this depends on the use case, the needs of the code and the approach in structuring it, but I am looking for a qualified understanding of it.
Thank you in advance.