Finding the PHP File (at run time) where a Class was Defined

前端 未结 3 1480
暗喜
暗喜 2020-11-28 22:14

Is there any reflection/introspection/magic in PHP that will let you find the PHP file where a particular class (or function) was defined?

In other words, I have the

相关标签:
3条回答
  • 2020-11-28 22:45

    For ReflectionClass in a namespaced file, add a prefix "\" to make it global as following:

    $reflector = new \ReflectionClass('FOO');

    Or else, it will generate an error said ReflectionClass in a namespace not defined. I didn't have rights to make comment for above answer, so I write this as a supplement answer.

    0 讨论(0)
  • 2020-11-28 22:57

    Try ReflectionClass

    • ReflectionClass::getFileName — Gets a filename

    Example:

    class Foo {}
    $reflector = new \ReflectionClass('Foo');
    echo $reflector->getFileName();
    

    This will return false when the filename cannot be found, e.g. on native classes.

    0 讨论(0)
  • 2020-11-28 23:01

    if you had an includes folder, you could run a shell script command to "grep" for "class $className" by doing: $filename = ``grep -r "class $className" $includesFolder/*\ and it would return which file it was in. Other than that, i don't think there is any magic function for PHP to do it for ya.

    0 讨论(0)
提交回复
热议问题