Should the "visibility" for the __destruct()
function be public or something else? I'm trying to write a standards doc for my group and this question came up.
In Addition to Mark Biek's answer:
The __destruct() function must be declared public. Otherwise, the function will not be executed on script shutdown:
Warning: Call to protected MyChild1::__destruct() from context '' during shutdown ignored in Unknown on line 0 Warning: Call to private MyChild2::__destruct() from context '' during shutdown ignored in Unknown on line 0
This may not be harmful, but rather unclean.
But the most important thing about this: If the destructor is declared private or protected, the runtime will throw a fatal error in the moment the garbage collector tries to free objects:
<?php class MyParent { private function __destruct() { echo 'Parent::__destruct'; } } class MyChild extends MyParent { private function __destruct() { echo 'Child::__destruct'; parent::__destruct(); } } $myChild = new MyChild(); $myChild = null; $myChild = new MyChild(); ?>
outputs
Fatal error: Call to private MyChild::__destruct() from context '' in D:\www\scratchbook\destruct.php on line 20
(Thanks to Mark Biek for the excellent example!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With