CodeIgniter - 自动加载
我想知道 CodeIgniter 自动加载的最佳实践是什么。自动加载我可能需要的所有内容是不好的做法还是可以这样做?这会给应用程序带来更多负载吗?
目前,我只是自动加载我将在整个应用程序中使用的库和助手:
$autoload['libraries'] = array('database', 'session', 'parser');
$autoload['helper'] = array('url', 'form');
所以我基本上想知道这是否“可以”做,或者我是否应该加载其中一些我实际上将使用它们(就像表单助手:仅当我实际要在页面上构建表单时才加载它)。
也许我在这里有点太偏执了,但是在阅读了很多关于“Ruby 方式”的文章后,我想知道是否有一种 CodeIgniter 方式。提前致谢!
I was wondering what the best practices for CodeIgniter's autoload are. Is it bad practice to just autoload everything I might ever need, or is it okay to do so? Does this put more load on the application?
At the moment I'm just autoloading the libraries and helpers I'll be using throughout my application:
$autoload['libraries'] = array('database', 'session', 'parser');
$autoload['helper'] = array('url', 'form');
So I'm basically wondering if this is 'okay' to do or if I should just load some of them when I'm actually going to use them (like the form helper: only load it when I'm actually going to build a form on a page).
Perhaps I'm being a bit too paranoid here, but having read about the 'Ruby way' a lot, I was wondering if there is, maybe, a CodeIgniter way. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$autoload 会加载一个资源,无论你是否使用它
因此,我只会自动加载你一直需要的东西,但如果我要输入我的 2 美分,我总是使用我的 __construct 来实现这一点在每个控制器类文件的顶部。这样,它会加载该类中的所有页面(函数),并且即使我在该类文件中不需要它,它也不会自动加载。
$autoload loads a resource weather you use it or not period
So that in mind I would only autoload what you need all the time, but if I were to input my 2 cents I always use my __construct for this in the top of each controller class file. That way it loads for all pages (functions) in that class and it isn't autoloading even if i don't need it in that class file.
您应该避免自动加载,因为它会影响您的性能。但是,如果您经常使用数据库连接,则可以自动加载它。
Php 不是 Java,因此加载的应用程序不会在请求之间持续存在。
You should avoid autoload, because it has an impact on your performance. However think like database connection could be autoloaded if you use it frequently.
Php isn't Java, so loaded application don't persist between requests.