Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current class and method?

Tags:

php

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?

like image 332
ajsie Avatar asked Jan 26 '10 15:01

ajsie


People also ask

How to get current running class name in java?

The simplest way is to call the getClass() method that returns the class's name or interface represented by an object that is not an array. We can also use getSimpleName() or getCanonicalName() , which returns the simple name (as in source code) and canonical name of the underlying class, respectively.

How do I get the current class in Python?

The first and easiest method to get a class name in python is by using __class__ property which basically refers to the class of the object we wish to retrieve. Here we combine the property with __name__ property to identify the class name of the object or instance.

How do you find the class of a method in SAP?

Go to SE16, enter table name SEOCOMPO and enter your method name for the field CMPNAME and execute. You will get all the classes that has this method. Remember, methods are not independent of its class. So the same method name can be implemented in different classes differently.

How do you find the current method name?

The current method name that contains the execution point that is represented by the current stack trace element is provided by the java. lang. StackTraceElement. getMethodName() method.


2 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__ 
like image 82
Antony Woods Avatar answered Sep 28 '22 03:09

Antony Woods


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) 
like image 37
SeanDowney Avatar answered Sep 28 '22 01:09

SeanDowney