문제

Is there a way for the function registered by spl_autoload_register to know the source file/class/method that's calling it? I want to be able to output a useful error when a class is not found so I know which source file needs to be updated. For example:

spl_autoload_register(function($className)
{
    $classFile = 'include/' . $className . '.php';
    if (!is_readable($classFile))
    {
        echo 'Could not load ' . $className . ' requested by ' . $source; 
        // how to figure out $source -----------------------------^^
        return false;
    }

    include $classFile;
    return false;
}
도움이 되었습니까?

해결책

That's what a stack trace does. It shows you the chain of events that lead to your error (and can provide details like class, line number, etc)

다른 팁

Try var dumping debug_backtrace() to see the array it returns and if it can helps.

spl_autoload_register(function($className)
{
    var_dump(debug_backtrace());

    ...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top