get_called_class() must be called from within a class

Description

get_called_class() returns the name of the current class. It emits an error when it cannot find the current class.

get_called_class() can only be called from within a class, trait or enumeration. It may be called from a static closure, as long as the closure is created inside a class.

Example

<?php

get_called_class();

?>

Solutions

  • Use the magic constant __CLASS__.

  • Use the function inside a class, enumeration or trait.