Clone A Constant¶
Cloning a constant was useless until PHP 8.1: this is the version where global constants could be initialized with an object.
The syntax has always been valid, but, at execution time, it would emit an error, as the constant could not be cloned.
PHP code¶
<?php
class C {}
const A = new C;
var_dump(clone A);
?>
Before¶
PHP Fatal error: Constant expression contains invalid operations
Fatal error: Constant expression contains invalid operations
After¶
object(C)#2 (0) {
}
PHP version change¶
This behavior changed in 8.1