简体   繁体   中英

Find a file containing a class (based on __autoload), but don't include it

I am using Composer to load PHP dependencies. Given the name of the class I want to find path of the file where this class is defined, but don't include it.

Example:

echo get_file_for_class('Carbon\\Carbon');

Output:

vendor/briannesbitt/Carbon/src/Carbon/Carbon.php

Thanks!

You could use the Reflection Class. http://php.net/manual/en/class.reflectionclass.php

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

There are some additional useful class methods you could also play around with, to determine its parent, if it inherits from one, or its start line.

$reflector->getParentClass();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM