Get current class and method?

Viewed 62510

I'm creating a log function that will log my errors in a file.

I thought it will contain which class and method the error occurred in.

Is there a way of logging in which class and method the error occurred in so I don't have to type it manually each time?

7 Answers

I'm not big on PHP but I believe it has "magic constants" similar to C/C++. Take a look here: This seems to indicate you could use

__LINE__, __FILE__, __FUNCTION__, __CLASS__, and __METHOD__

use the __METHOD__ constant in PHP5

In Laravel 5 CLASS was returning namespace and class name, so it was a large string. So this is how you get current Class without all that other stuff:

echo (new \ReflectionClass($this))->getShortName();

Since PHP 8.0 there is new way if variable is an object:

$object::class

$this::class
Related