在加载类时,PHP的use语句是否会引起额外的工作? [英] Does PHP's use statement cause extra work when loading classes?

查看:251
本文介绍了在加载类时,PHP的use语句是否会引起额外的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码示例1

use Outline\Drawing;
$var = new Drawing();

代码示例2

$var = new Outline\Drawing();

问题:

如果我在示例1中使用代码,PHP是否会使硬件工作更困难(查找更多文件或进行更多处理)?我确定已经完成了某些事情,即使它是在弄清楚哪个use行与哪个类相匹配的代码级别上也是如此.我想确切地了解正在发生的事情.

Does PHP make hardware work harder (look up more files or do more processing) if I use code in sample 1? I am sure something gets done, even if it is at the level of some code that figures out which use line gets matched up with which class. I want to find out exactly what is happenning.

简而言之:

  • 在计算use语句的使用与它应该用于的类之间的联系时,PHP会做什么?
  • 当涉及这两个代码样本时,PSR-0/PSR-4自动加载器是否会影响它们的工作方式?
  • What does PHP do when working out the connection between the use of the use statement and a class it is supposed to be for?
  • Are PSR-0/PSR-4 autoloaders affected in the way they work when it comes to these two code samples?

推荐答案

在计算use语句的使用与它应该用于的类之间的联系时,PHP会做什么?

What does PHP do when working out the connection between the use of the use statement and a class it is supposed to be for?

use语句实际上并未将名称空间/类加载到文件中.它只是设置了一个别名列表来引用该命名空间中的类.

The use statement doesn't actually load the namespace/class into your file. It simply sets up a list of aliases to refer to the classes in that namespace.

当遇到尚未声明的类时,它将使用该别名列表尝试完全限定类名(替换前缀).如果找不到该类的别名,它将使用当前作用域的名称空间来限定该类的名称.

When it encounters a class that hasn't yet been declared, it uses that list of aliases to try to fully qualify the class name (prefix replacement). If it can't find an alias for the class, it'll use the namespace of the current scope to qualify the class name.

仅当类名完全合格时,该php才会尝试自动加载该类(调用可能已经定义的各种自动加载器).

Only when the class name is fully qualified, that php will try to autoload the class (calling the various autoloaders that might have been defined).

在涉及这两个代码示例时,PSR-0/PSR-4自动加载器是否会影响它们的工作方式?

Are PSR-0/PSR-4 autoloaders affected in the way they work when it comes to these two code samples?

不,自动加载器不会受到代码示例差异的影响,因为php会使用完全相同的参数以完全相同的方式调用自动加载器.

No, autoloaders are not affected in the way they work by the difference in your code samples because php will call the autoloaders exactly the same way with exactly the same parameters.

这篇关于在加载类时,PHP的use语句是否会引起额外的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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