:():` * @return string|array */ function here(bool $as_array = false): string|array { $trace = debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 2); return $as_array ? [ 'from' => $trace[1]['class'] ?? $trace[0]['file'], 'function' => $trace[1]['function'], 'line' => $trace[0]['line'], ] : sprintf( '%s%s%s():%s', $trace[1]['class'] ?? $trace[0]['file'], $trace[1]['type'] ?? '::', $trace[1]['function'], $trace[0]['line'] ); } // Usage: class MyClass { public function test(): string { return here(); } } echo (new MyClass)->test(); // MyClass->test():4