php spl_autoload_register与__autoload吗? [英] php spl_autoload_register vs __autoload?

查看:89
本文介绍了php spl_autoload_register与__autoload吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,使用此方法有什么区别,除了我们可以使用自己的名称自动加载?有任何性能差异吗?他们在内部如何工作?

hello is there any diffrence useing this excepts that we can use our own name auto load? is there any performance difference? how do they internally work?

之间

function __autoload_libraries($class){
    include_once 'lib.'.$class.'.php';
}

spl_autoload_register('__autoload_libraries');

vs

function __autoload($class){
    include_once 'lib.'.$class.'.php';
}

推荐答案

__autoload通常被认为已过时.它仅允许使用一个自动装带器.通常,只有在使用不支持spl_autload_register的PHP版本时,才应使用__autoload.

__autoload is generally considered obsolete. It only allows for a single autoloader. Generally you should only use __autoload if you're using a version of PHP without support for spl_autload_register.

spl_autoload_register允许注册多个自动加载器,这些自动加载器将依次运行,直到找到并加载匹配的类/接口/特征,或者直到用尽所有自动加载选项为止.这意味着,如果您使用的是框架代码或其他实现自己的自动加载器的第三方库,则不必担心会引起冲突.

spl_autoload_register allows several autoloaders to be registered which will be run through in turn until a matching class/interface/trait is found and loaded, or until all autoloading options have been exhausted. This means that if you're using framework code or other third party libraries that implement their own autoloaders you don't have to worry about yours causing conflicts.

__autoload现在正式被弃用,这意味着它已经在斩波块上了.如果您希望代码与PHP的未来版本兼容,则绝对不应使用__autoload

__autoload is now officially deprecated as of PHP 7.2.0, which means it's now on the chopping block. If you want your code to be compatible with future versions of PHP you definitely should not use __autoload

这篇关于php spl_autoload_register与__autoload吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆