PHP 5.5类名解析 [英] PHP 5.5 Classname Resolution

查看:175
本文介绍了PHP 5.5类名解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP 5.5实现了一种新的方法,通过语法 :: class 检索类名:

PHP 5.5 has implemented as a new feature a new way to retrieve the classname through the syntax ::class:

<?php

namespace Testing;

class Test{}

echo Test::class; // Testing\Test;

这完美地工作,好吗? BUt什么我和一些其他朋友想知道的是为什么这种语法也返回一个类名,当与未声明的类一起使用。例如:

This works perfectly, alright? BUt what me and some other friends wanted to know is why this syntax also returns a classname when used alongside an undeclared class. E.g.:

<?php

echo UndeclaredClass::class; // UndeclaredClass

在其他几种情况下,会出现错误,任何人都知道,为什么会发生?为什么会发生这种情况?

In several other cases an error is raised, but not here. Anyone know, with concrete basis if possible, why does this happen?

它有什么东西静态绑定或它只是一个新功能?

Does it have anything to Late Static Bindings or it's just a (temporary) limitation/bug of this brand new feature?

推荐答案

最后一个官方回答...相对来说。在 bu报表 中由 requinix@php.net / a> i今天创建。唯一的例外是这个人如何参与PHP开发。

Finally an official answer... relatively speaking. It was presented to me by someone identified by requinix@php.net in a bu report i created today. The only exception is about how involved with PHP development this person is.


PHP不需要知道类的定义以获取其完全限定名称。

PHP doesn't need ot know the definition of a class to get its fully-qualified name. All the required informations are available in compile-time so it doesn't need to load it.



导演剪辑

$像编译时解决的 b
$ b

命名空间其执行。这就是为什么有严格的要求,以使用它们。

Director's Cut

Namespaces like the uses are resolved in compile-time, i.e., when the file is compiled before its execution. That's why there are strict requirements in order to use them.

由于所有这些要求,当PHP遇到一个类名称,它可以立即知道其完全限定名称。将其视为文件系统,命名空间将是相对位置的目录, 将是符号链接

Because of all of those requirements, when PHP encounters a class name it can immediately know its fully-qualified name. Thinking of it as a filesystem the namespace would be a directory for relative locations and the use would be symlinks.

类名称是绝对的(\Testing\Test)或相对的(测试),如果相对,它可以是一个正常的名称。 [需要更多的上下文]

The class name is either absolute ("\Testing\Test") or relative ("Test"), and if relative it could be a normal name. [more context required]

namespace Testing {
    echo Test::class; // \Testing + Test = \Testing\Test
}

use Testing\Test as AliasedTest;
echo AliasedTest::class; // AliasedTest + use = \Testing\Test

没有这些 / em>不会工作!

Without all of this autoloading wouldn't work!

:: class 只是一个公开信息的新工具已知。

::class is just a new tool to expose information PHP has always known.


这个扩展答案几乎与我收到的错误报告相同。这么多明显的复制&粘贴是因为,最初,我建立这个答案另一个Stack Overflow社区

This "extended answer" is pretty much the same of what I received as bug report. The reason of so much apparent copy & paste is because, originally, I built up this answer for another Stack Overflow Community

这篇关于PHP 5.5类名解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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