What is the best practice for determining the name of the function that instantiated a class in PHP?

When working with classes in PHP, it can be useful to determine the name of the function that instantiated a particular class instance. One way to achieve this is by using the debug_backtrace() function, which provides information about the current call stack. By examining the backtrace, you can identify the function that created the object.

class MyClass {
    public function __construct() {
        $trace = debug_backtrace();
        $caller = $trace[1]['function'];
        echo "This object was instantiated by the function: $caller";
    }
}

function myFunction() {
    $obj = new MyClass();
}

myFunction();