Call to %s %s::__destruct() from global scope during shutdown ignored¶
Description¶
During PHP shutdown, destructors are called for remaining objects. When a __destruct() method is invoked from global scope (i.e. not as part of object destruction triggered by the garbage collector or script end), PHP emits this warning and ignores the call.
This typically happens when a register_shutdown_function() callback creates an object whose destructor is called during the final cleanup phase. The object is destroyed immediately, but the invocation from global scope is considered invalid.
The warning ensures that destructors are only called in their expected context: as part of object lifetime management, not from arbitrary global code.
Example¶
<?php
class X {
public function __destruct() {
echo "Destroying\n";
}
}
register_shutdown_function(function() {
$x = new X;
});
?>
Literal Examples¶
Call to public X::__destruct() from global scope during shutdown ignored
Call to protected X::__destruct() from global scope during shutdown ignored
Solutions¶
Move the logic from
__destruct()into a regular method and call it explicitly before the object is destroyed.Avoid creating objects inside shutdown functions if you rely on their destructor behavior.
Use a dedicated lifecycle method instead of relying on
__destruct()for cleanup logic.
See Also¶
Changed Behavior¶
This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.