PHP: Find the file where a class was defined

There is a mobile optimized version of this page, view AMP Version.

It's annoying when you are looking for a particular class in a system you are not very familiar with, you end up looking all over the system file by file hoping to find the class so you can make changes to the code as you require.

Well there is a simple solution which will allow you find the file where the class is defined, using the reflector class we can pinpoint the exact path to the script file that defines a given class.

All you need to do is place this in the code near where you are calling the class:

$reflector = new ReflectionClass('CLASSNAME');
echo $reflector->getFileName();

Just replace CLASSNAME with the name of the class you wish to track down.

Author: Dean Williams

I'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips

Post Tags: