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.

<?php
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

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

Here is the outcome from above code:

/path_to_word_press_root/wp-includes/query.php


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

Comments

Click here to write/view comments