In my experience, destructors will always be called in PHP 5.3, but keep in mind that if some piece of code calls exit () or a fatal error occurs, PHP will call the destructors in "any" order (I think the actual order is the order in memory or memory order reserved for objects. In practice, this order is almost always problematic). This is referred to as the โshutdown sequenceโ in the PHP documentation.
PHP destructor documentation :
PHP 5 introduces the concept of a destructor, similar to the concept of other object-oriented languages, such as C ++. The destructor method will be called as soon as there are no other references to a specific object or in any order during the shutdown sequence.
As a result, if you have a class X that contains a reference to Y, the destructor X can be called AFTER the destructor Y has already been called. I hope the link to Y was not so important ... Officially, this is not a mistake, because it has been documented.
However, it is very difficult to get around this problem, because officially PHP does not allow you to find out if the destructor is called normally (destructors are called in the correct order), or destructors are called in "any" order, where you cannot use data from reference objects because they are already were destroyed. You could get around this lack of detection with debug_backtrace () and examine the stack. The lack of a normal stack seems to imply a "shutdown sequence" with PHP 5.3, but it is also undefined. If you have circular references, the destructors of these objects will not be called at all with PHP 5.2 or less and will be called in "any" order during the "shutdown sequence" in PHP 5.3 or higher. For circular links, there is no logically โcorrectโ order, so โanyโ order is good for them.
There are some exceptions (after all, this is PHP):
- If
exit() is called in another destructor, any remaining destructors will not be called ( http://php.net/manual/en/language.oop5.decon.php ) - If a
FATAL error occurs somewhere (many possible reasons, such as trying to remove an exception from any other destructor, may be one reason)
Of course, if the PHP engine encounters a segmentation error or some other internal error, then all bets are disabled.
Mikko Rantalainen Nov 28 2018-11-11T00: 00Z
source share