PHP – get class name and file name from an object

PHP – get class name of an object

get_class with object as arguments returns its class name.

class Class1 {
$obj = new Class1();
echo "class name of obj=" . get_class($obj);
class name of obj=Class1
Env: PHP 5.5.9 (Linux)

PHP – get filename from class name

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

Here is the outcome from above code:


Note: This can be useful to debug large code base.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments