我使用php和codeigniter,它有自动加载类。我的第一个问题是,
If I use autoload class to load all model will it make my application slower? Or is there no effect?
我的第二个问题:
Which one is better and faster, loading all model you need using autoload class, Or load only some models you need in a class function?
1)自动加载类显然会使应用程序速度变慢。因为它使用php4-require函数来加载文件。有一些使用php5自动加载功能的技巧。希望,代码点火器的新所有者将添加对自动加载的支持。
2) 最好使用特定于负载的模型,而不是自动加载。在前一点中,我阐述了这背后的原因。基本上,只加载所需的模型、助手、库和资产是一种很好的做法。它确保您使用最少的时间和内存。
我使用自动加载。它的工作就像一个魅力,对加载时间没有显著影响。
方法
在CI autoload.php
中使用的任何库/模型上添加此代码
例如,我的config/autoload.php看起来像
$autoload['libraries'] = array('database','door','acl','form_validation','notify');
在图书馆/Door.php中,我添加了
<?php//libraries/Door.php
function multi_auto_require($class) {
#var_dump("requesting $class");
if(stripos($class, 'CI') === FALSE && stripos($class, 'PEAR') === FALSE) {
foreach (array('objects','core') as $folder){//array of folders to look inside
if (is_file(APPPATH."{$folder}/{$class}.php")){
include_once APPPATH."{$folder}/{$class}.php";
}
}
}
}
spl_autoload_register('multi_auto_require');
class Door {
我在class Door{
上面添加了这个片段,这样每当codeigniter加载door库时,这个片段都会运行。
测试和基准测试现在,为了进行基准测试,我在一个页面中测试了这段代码,该页面包含来自2个不同文件夹的8个自动加载对象的17个DB查询,其中有3个文件夹要查找。
结果对于所有项目类,使用上述方法Vs include_once 'classlocation.php'
,
两种方法的平均10页刷新时间约为0.6秒;因此,您可以看到这两种方法之间没有显著差异。
尽管我没有在不使用所有类的页面上测试它,但我确信自动加载会让我的CI生活变得更好,我对此感到满意。