Get current class and method?

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?


Solution 1:

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__

Solution 2:

In the event your in a parent / base class, __CLASS__ will return the parent / base class name which is not desired. In that event you can use get_class():

get_class($this)

Solution 3:

In current PHP versions (5.5+) you should use static::class

It works both in static and instance methods and returns the actual class name, even if the method body was defined in a superclass.

Solution 4:

use the __METHOD__ constant in PHP5