Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__destruct visibility for PHP

Tags:

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.

like image 500
Clutch Avatar asked Oct 23 '08 15:10

Clutch


1 Answers

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!)

like image 118
Dan Soap Avatar answered Oct 25 '22 07:10

Dan Soap