在 PHP 中定义自动加载器函数的最佳方法是什么,该函数可以显示在找不到类时是哪一行导致调用该函数?
Atm 我有一个简单的自动加载器,它只是将下划线转换为前斜杠并粘贴在最后的 .php 上。当然,这需要您将“应用程序目录”添加到包含路径中。
当您尝试使用找不到的类时,就会出现此问题。在这种情况下,PHP 只会发出一些微不足道的警告和错误,这些警告和错误仅指向我的自动加载器函数作为源。
找出哪个文件中的哪一行导致自动加载器尝试加载丢失的类的最简单方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是我最终的做法,稍作修改:
这允许您将“main”函数包装在 try 块中,您可以在其中捕获这些异常并从异常中获取 stackTrace 并在
Here's how I ended up doing it, slightly modified:
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.添加类似这样的内容:
或者更详细:
Add something like this:
or do it more detailed: