Constants With Objects¶
Global constants are allowed to use an object, starting with PHP 8.1. The object must be instantiated with only constants values, like literals and other constants.
Class constant are not allowed to use the new keyword.
PHP code¶
<?php
const A = new stdclass();
var_dump(A);
?>
Before¶
PHP Fatal error: Constant expression contains invalid operations
Fatal error: Constant expression contains invalid operations
After¶
object(stdClass)#1 (0) {
}
PHP version change¶
This behavior changed in 8.1