'use' 关键字是否会触发 PHP 中的自动加载? [英] Does the 'use' keyword trigger autoloading in PHP?

查看:57
本文介绍了'use' 关键字是否会触发 PHP 中的自动加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,自动加载器在什么时候加载类文件(如果有的话)?例如,如果 $boolean_test === false 是否加载了子包?

In the example below, at what point does the autoloader load the class file, if at all? For example, if $boolean_test === false does the Subpackage get loaded?

use OrgGroupPackageSubpackage; // autoloader triggered here?
if ($boolean_test) {
    Subpackage::method(); // or here?
}

我更喜欢代码顶部附近的 use 语句,这样我就可以看到页面中使用了哪些包,并稍微提高了可读性.但是,如果仅根据条件使用包,我可能会加载不需要的资源.

I prefer the use statement near the top of the code so I can see what packages are used in the page and for slightly better readability. But, if packages are only used based on conditionals, I may be loading unneeded resources.

推荐答案

No use 不会触发自动加载.您可以在文件顶部设置无效的 use,PHP 不会抱怨.

No use does not trigger autoloading. You can have an invalid use at the top of your file and PHP won't complain.

当您尝试使用该类时会发生自动加载.

Autoloading happens when you attempt to use the class.

测试这个很容易:https://3v4l.org/OccF3

这篇关于'use' 关键字是否会触发 PHP 中的自动加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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