autoload and namespaces

你离开我真会死。 提交于 2019-12-03 17:20:31

The autoloader will get foo\bar\baz\Quux as the class name argument.

Namespace name definitions
Unqualified name
This is an identifier without a namespace separator, such as Foo

Qualified name
This is an identifier with a namespace separator, such as Foo\Bar

Fully qualified name
This is an identifier with a namespace separator that begins with a namespace separator, such as \Foo\Bar. namespace\Foo is also a fully qualified name.

And the the rule is :

All unqualified and qualified names (not fully qualified names) are translated during compilation according to current import rules. For example, if the namespace A\B\C is imported as C, a call to C\D\e() is translated to A\B\C\D\e().

You will get a fully qualified namespace name (without leading backslash).

This also applies to all other dynamic language constructs and functions in PHP:

If you write $obj = new $class the $class must be fully qualified and doesn't use any defined aliases.
If you write class_exists($class) the $class must also be fully qualified.
And same applies to autoload: It is passed the fully qualified name.

(The only exception to this rule I know of is the define function, which can define constants both using fully qualified names, but also using an unqualified name, in which case it will be defined in the current namespace.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!