PHP 5.5已经实现了一个新的特性,通过语法::class来获取::class :
<?php namespace Testing; class Test{} echo Test::class; // Testing\Test;这工作完美,好吗? 请问我和其他一些朋友想知道的是,为什么这个语法在与未声明的类一起使用时也会返回一个类名。 例如:
<?php echo UndeclaredClass::class; // UndeclaredClass在其他几种情况下会出现错误,但不在此处。 任何人都知道,如果可能的话,具体的基础,为什么会发生?
它是否对Late Static Bindings有任何意义,或者它只是这个全新功能的(临时)限制/缺陷?
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;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; // UndeclaredClassIn 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标识的人向我呈现的。 唯一的例外是关于这个人是如何参与PHP开发的。
TL; DR
PHP不需要知道类的定义以获取其完全限定的名称。 所有必需的信息都可以在编译时使用,因此不需要加载它。
导演剪辑版
像这些用途这样的命名空间在编译时被解析,也就是说,当文件在执行前被编译时。 这就是为什么有严格的要求才能使用它们。
由于所有这些要求,当PHP遇到类名时,它可以立即知道它的完全限定名。 把它看作一个文件系统的命名空间将是一个相对位置的目录,并且使用 符号链接 。
类名称是绝对的(“\ Testing \ Test”)或相对的(“Test”),如果是相对的,它可以是普通名称。 [需要更多上下文]
namespace Testing { echo Test::class; // \Testing + Test = \Testing\Test }或别名 :
use Testing\Test as AliasedTest; echo AliasedTest::class; // AliasedTest + use = \Testing\Test如果没有所有这些自动加载都不行!
::class只是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.
TL;DR
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.
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.
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.
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 }Or an alias:
use Testing\Test as AliasedTest; echo AliasedTest::class; // AliasedTest + use = \Testing\TestWithout all of this autoloading wouldn't work!
::class is just a new tool to expose information PHP has always known.
PHP 5.5类名解析(PHP 5.5 Classname Resolution)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已经实现了一个新的特性,通过语法::class来获取::class :
<?php namespace Testing; class Test{} echo Test::class; // Testing\Test;这工作完美,好吗? 请问我和其他一些朋友想知道的是,为什么这个语法在与未声明的类一起使用时也会返回一个类名。 例如:
<?php echo UndeclaredClass::class; // UndeclaredClass在其他几种情况下会出现错误,但不在此处。 任何人都知道,如果可能的话,具体的基础,为什么会发生?
它是否对Late Static Bindings有任何意义,或者它只是这个全新功能的(临时)限制/缺陷?
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;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; // UndeclaredClassIn 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标识的人向我呈现的。 唯一的例外是关于这个人是如何参与PHP开发的。
TL; DR
PHP不需要知道类的定义以获取其完全限定的名称。 所有必需的信息都可以在编译时使用,因此不需要加载它。
导演剪辑版
像这些用途这样的命名空间在编译时被解析,也就是说,当文件在执行前被编译时。 这就是为什么有严格的要求才能使用它们。
由于所有这些要求,当PHP遇到类名时,它可以立即知道它的完全限定名。 把它看作一个文件系统的命名空间将是一个相对位置的目录,并且使用 符号链接 。
类名称是绝对的(“\ Testing \ Test”)或相对的(“Test”),如果是相对的,它可以是普通名称。 [需要更多上下文]
namespace Testing { echo Test::class; // \Testing + Test = \Testing\Test }或别名 :
use Testing\Test as AliasedTest; echo AliasedTest::class; // AliasedTest + use = \Testing\Test如果没有所有这些自动加载都不行!
::class只是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.
TL;DR
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.
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.
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.
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 }Or an alias:
use Testing\Test as AliasedTest; echo AliasedTest::class; // AliasedTest + use = \Testing\TestWithout all of this autoloading wouldn't work!
::class is just a new tool to expose information PHP has always known.
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
发布评论