PHP 多个 __autoload 函数*不*使用 spl_register_autoload?
我是一个不断增长的 PHP + QuickBooks 相关代码库的作者。我想利用 PHP 的 __autoload() 函数,但是我的代码是一个库,其他人可以将其 include() 到他们自己的应用程序中,所以我不能依赖 __autoload() 尚未实现定义的。
有没有办法拥有多个 __autoload() 函数?
我在 PHP 手册中看到了 spl_autoload_register(),但是并不是我的所有用户都安装了 SPL 扩展,所以我不能依赖它。如果有一种方法可以回退到使用它并默认使用正常的 require/include 语句,我可能会考虑这样做。
还有其他人可以解决这个问题吗?只能有一个 __autoload() 函数似乎是一个明显的疏忽......
I'm an author of a growing library of PHP + QuickBooks related code. I'd like to utilize PHPs __autoload() function, however my code is a library that other people can include() into their own applications, so I can't rely on __autoload() being not already defined.
Is there a way to have multiple __autoload() functions?
I saw spl_autoload_register() in the PHP manual, but not all of my users have the SPL extension installed, so I can't rely on that. If there was a way to fall-back to using this and use normal require/include statements by default, I might consider that.
Does anyone else have any other clever solutions to this issue? It seems like a glaring oversight to only be able to have a single __autoload() function...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
忽略它们,它是默认编译的,甚至无法在 5.3.0 中禁用。不要为了取悦少数人而牺牲你的框架。
Ignore them, it's compiled in by default and can't even be disabled in 5.3.0. Don't sacrifice your framework to please the minority.
我决定解决这个问题并保持向后兼容性的方法是编写我自己的“Loader”类。
我现在使用 Loader::load('TheFile.php');,而不是使用 require_once 'TheFile.php'。 Loader::load() 方法执行以下操作:
如果 PHP 安装支持自动加载器,这使我可以灵活地使用自动加载器,否则只能执行正常的 require $file 操作;类型的东西。
I have decided that the way to go about this and also preserve backward compatibility is to write my own "Loader" class.
Instead of using require_once 'TheFile.php', I now use Loader::load('TheFile.php');. The Loader::load() method does the following:
This gives me the flexibility to use the autoloader if their PHP installation supports it, and otherwise fall back to just doing normal require $file; type stuff.
您可以编写一个 __autoload 函数,它简单地循环执行实际的类加载的回调数组,直到找到该类或直到列表耗尽。
注意:这是“即兴的”,我还没有测试过。
You could write an __autoload function that simply loops over an array of callbacks that do the actual class loading until the class has been found or until the list has been exhausted.
NOTE: This is "off the cuff", I've not tested it.