dourenzhou8740 2011-03-06 04:30
浏览 30
已采纳

哪个是更好的PHP自动加载方法?

Would the second autoload method I have below be better performance?

<?php

// regular __autoload function
function __autoload( $name )
{
     include( $class . '.php' );

}


// SPL autoload
spl_autoload_register("MyClass::Autoloader");
class MyClass
{
    public static function Autoloader($class)
    {
        // list of classes and their location
        $classes = array();
        $classes['spooncookie'] = 'cookie/cookie.php';
        $classes['spoondatabase'] = 'database/database.php';
        $classes['spoondatagrid'] = 'datagrid/datagrid.php';
        $classes['spoondatagridcolumn'] = 'datagrid/column.php';
        $classes['ispoondatagridpaging'] = 'datagrid/paging.php';
        // ....list of all the other class files.......    
        if(isset($classes[$class])){
            include( $classes[$class] );
        }
    }

}

?>
  • 写回答

3条回答 默认 最新

  • duanaozhong0696 2011-03-06 04:36
    关注

    The best way to get an accurate idea of which will give the best performance for your own usage is to test each of them. As a general rule, different methods of autoloading will be optimal in different scenarios, factors include: how many files/classes you have in total, how deep the directory structure is, and how many classes you will load on average per request. The key point behind using an autoloader isn't to make your code run more quickly, but to improve how quickly you can write the code in the first place.

    For production code I really would recommend going with a well used autoloader from a framework instead of rolling your own, at least to start with. and then maybe modifying it later if you know its the bottleneck and can be improved.

    As a side note, have you seen this? http://weierophinney.net/matthew/archives/245-Autoloading-Benchmarks.html

    EDIT:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度