Reputation: 15310
Atm I have a simple autoloader that just converts underscores to front slashes and sticks on a .php at the end. Naturally this requires you to add your "app dir" to the include path.
The problem appears when you attempt to use a class that cannot be found. In this case PHP will just emit some measly Warnings and Errors that only point to my autoloader function as the source.
What is the easiest way to find out which line in which file caused the autoloader to attempt to load the missing class?
Upvotes: 0
Views: 139
Reputation: 15310
Here's how I ended up doing it, slightly modified:
function classNameToPath($className) {
// Add your code which converts a class name to a relative path and returns it
// For example Databases_Connection => Databases/Connection.php
}
function __autoload($className) {
$classFile = classNameToPath($className);
$allIncludePaths = preg_split("/:/", get_include_path());
foreach($allIncludePaths as $includePath) {
if (file_exists("${includePath}/${classFile}")) {
require_once($classFile);
return;
}
}
throw new Exception("Class not found {$clasSName}");
}
This allows you to wrap your "main" function in a try block, where you can catch these exceptions and get the stackTrace from the exception and echo it inside of <pre>
elements for example. Gotta love that you have to code all these things yourself in PHP ;) Well even if you don't catch the class not found exceptions PHP will still catch them and automatically display the stacktrace, just not within <pre>
so it will be a big mess on one line if you see it in your browser.
Upvotes: 0
Reputation: 41050
Add something like this:
if (!file_exists($class.'.php')) {
echo '<pre>';
debug_print_backtrace();
echo '</pre>';
}
or do it more detailed:
if (!file_exists($class.'.php')) {
// PSEUDO CODE!
echo '<pre>';
$debug = debug_backtrace();
echo 'Line: '.$debug['line'];
echo '</pre>';
}
Upvotes: 3