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

查看:26
本文介绍了php spl_autoload_register vs __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');

对比

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

推荐答案

__autoload 通常被认为已过时.它只允许一个自动加载器.通常,如果您使用的 PHP 版本不支持 spl_autload_register,您应该只使用 __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 7.2.0 开始正式弃用,这意味着它现在处于砧板中.如果你想让你的代码与 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 vs __autoload?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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