Alex
Alex

Reputation: 68084

Get the file name of a instantiated class

Let's say I got the class name with get_class($this) within an abstract class method.

How can I also get the file name in which this class was defined? (full path)

I know I could pass it as an argument to my child class and create a property which is accessible in the parent class, but I was wondering if PHP has something built-in

Upvotes: 4

Views: 357

Answers (1)

Jeshurun
Jeshurun

Reputation: 23186

What you need is the ReflectionObject class and its method, ReflectionClass::getFileName.

$reflection_class = new ReflectionClass(get_class($this));
echo $reflection_class->getFileName();

Check the function's manual here.

Upvotes: 5

Related Questions