问题
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