get_called_class() Cannot Be Called Outside A Class¶
Until PHP 8, get_called_class() generated a warning when called outside a class or an enumration. Later, it is a fatal error.
PHP code¶
<?php
var_dump(get_called_class());
?>
Before¶
PHP Warning: get_called_class() called from outside a class in /codes/get_called_class_outside_class.php on line 3
Warning: get_called_class() called from outside a class in /codes/get_called_class_outside_class.php on line 3
bool(false)
After¶
PHP Fatal error: Uncaught Error: get_called_class() must be called from within a class in /codes/get_called_class_outside_class.php:3
Stack trace:
#0 {main}
thrown in /codes/get_called_class_outside_class.php on line 3
Fatal error: Uncaught Error: get_called_class() must be called from within a class in /codes/get_called_class_outside_class.php:3
Stack trace:
#0 {main}
thrown in /codes/get_called_class_outside_class.php on line 3
PHP version change¶
This behavior was deprecated in 7.0
This behavior changed in 8.0