Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Making A Destructor A Generator Is Deprecated

Using yield anywhere in the body of __destruct() turns it into a Generator function. A Generator only starts executing once something iterates it, but nothing iterates the return value of a destructor: PHP just calls it during garbage collection and discards the result. As a consequence, the body of such a destructor never actually runs, silently skipping any cleanup code it contains. Since this pattern is virtually always a mistake, PHP 8.6 deprecates making a destructor a Generator.

PHP code

<?php

class X {
    public function __destruct() {
        yield 1;
    }
}

$x = new X;
unset($x);

?>

Before


After

PHP Deprecated:  Making a destructor a Generator is deprecated in /codes/destructorAsGenerator.php on line 4

Deprecated: Making a destructor a Generator is deprecated in /codes/destructorAsGenerator.php on line 4

PHP version change

This behavior was deprecated in 8.6.

This behavior changed in 8.6.

See Also

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat