Making A Constructor A Generator Is Deprecated
Using yield anywhere in the body of __construct() turns it into a Generator function. Because a Generator’s body only starts running once it is iterated, and nothing iterates a constructor, the code inside such a constructor never runs when the object is created with new: initialization is silently skipped. Since this pattern is virtually always a mistake, PHP 8.6 deprecates making a constructor a Generator.
PHP code
<?php
class X {
public function __construct() {
yield 1;
}
}
new X;
?>
Before
After
PHP Deprecated: Making a constructor a Generator is deprecated in /codes/constructorAsGenerator.php on line 4
Deprecated: Making a constructor a Generator is deprecated in /codes/constructorAsGenerator.php on line 4
PHP version change
This behavior was deprecated in 8.6.
This behavior changed in 8.6.