Does the 'use' keyword trigger autoloading in PHP?

折月煮酒 提交于 2019-12-31 01:56:10

问题


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 Org\Group\Package\Subpackage; // autoloader triggered here?
if ($boolean_test) {
    Subpackage::method(); // or here?
}

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.


回答1:


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.

It's quite easy to test this: https://3v4l.org/OccF3



来源:https://stackoverflow.com/questions/35755243/does-the-use-keyword-trigger-autoloading-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!