哪种 PHP 自动加载方法更好?

发布于 2024-10-20 15:46:08 字数 839 浏览 2 评论 0原文

我下面的第二种自动加载方法性能会更好吗?

<?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] );
        }
    }

}

?>

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] );
        }
    }

}

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扬花落满肩 2024-10-27 15:46:08

要准确了解哪一个能够为您自己的使用提供最佳性能,最好的方法是对它们进行测试。一般来说,不同的自动加载方法在不同的场景中是最佳的,因素包括:总共有多少个文件/类、目录结构的深度以及每个请求平均加载多少个类。
使用自动加载器的关键不是让代码运行得更快,而是首先提高编写代码的速度。

对于生产代码,我真的建议使用框架中使用良好的自动加载器,而不是自己滚动,至少在开始时是这样。如果您知道它是瓶颈并且可以改进,那么稍后可能会对其进行修改。

作为旁注,你见过这个吗?
http://weierophinney.net/matthew/archives/245-Autoloading-Benchmarks.html编辑

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:

云朵有点甜 2024-10-27 15:46:08

除非您已将自动加载器(以及底层文件系统)确定为性能瓶颈,否则您真的不应该担心它。 配置文件,配置文件,配置文件!自动加载器可能是一个真正的阻力,但你很可能通过实际对代码进行基准测试来在其他地方找到更大的性能提升。

老实说,我会选择PSR-0新项目的兼容自动加载器

Unless you have identified your autoloader (and the underlying filesystem poking and prodding) as a performance bottleneck, you really shouldn't worry about it. Profile, profile, profile! Autoloaders can be a real drag, but you are likely to find a larger performance boost elsewhere by actually benchmarking your code.

To be honest, I'd go for a PSR-0 compliant autoloader for a new project.

云雾 2024-10-27 15:46:08

我倾向于选择 spl 方法,因为某些库(例如 Smarty)实现了 spl 自动加载器,因此使用 __autoload 方法可能会导致冲突。

函数内路径检查也是一个好主意,因为任何将文件拉入程序的函数都有潜在危险,特别是在启用了allow_url_fopen的环境中!由于这些原因,我倾向于使用带有路径检查的 SPL 函数。

我可能建议的一项优化是使路径数组静态。这应该消除每次调用函数时创建数组的开销(尽管事实上这样的开销无论如何应该很小)

I'd be inclined to opt for the spl method on the grounds that certain libraries such as Smarty implement an spl autoloader so using the __autoload method can result in conflicts.

The in-function path checking is a good idea too, because any function that pulls a file into your program is potentially dangerous, especially in an environment with allow_url_fopen enabled! I'd be inclined to go with your SPL function complete with its path checking for these reasons.

One optimisation I might suggest is making your path array static. That should eliminate the overhead of creating the array every time the function is envoked (though in truth such overhead should be pretty tiny anyway)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文