Import class conditionally with the keyword #39;use#39;(使用关键字“使用有条件地导入类)
问题描述
我从来没有在任何地方看到过这种结构,所以我想知道这样的表达式是否有问题:
I've never seen this structure anywhere, so I wonder if there's something wrong with an expression like this:
if (condition) {
use SymfonyComponentHttpFoundationResponse;
}
推荐答案
use 唯一要做的就是别名一个类名.而已.仅此而已.
不必在脚本中重复编写完全限定的类名:
The only thing use does is to alias a class name. That's it. Nothing more.
Instead of having to repeatedly write the fully qualified classname in your script:
$q = new FooBarBazQuux;
if ($q instanceof FooBarBazQuux) ...
您可以将其缩短为:
use FooBarBazQuux;
$q = new Quux;
if ($q instanceof Quux) ...
因此,想要有条件地使用 use 绝对没有意义.它只是一个语法助手;如果可以有条件地使用它,您的 脚本语法 会变得模棱两可,这是没有人想要的.
As such, it makes absolutely no sense to want to use use conditionally. It's just a syntactic helper; if it could be used conditionally your script syntax would become ambiguous, which is something nobody wants.
它不会减少代码加载,因为代码只能通过 require/include 调用或通过自动加载显式加载.后者是非常受欢迎的,因为它已经懒惰地只在需要时才开始行动.
It doesn't reduce code loading, because code is only loaded explicitly by require/include calls or via autoloading. The latter one is greatly preferred, since it already lazily springs into action only when needed.
这篇关于使用关键字“使用"有条件地导入类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用关键字“使用"有条件地导入类
基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
