PHP 7 短标准命名空间的组使用声明
PHP 7 Group Use Declarations for Short Standard Namespaces
所以 PHP 7 有很好的命名空间组使用声明,比如 this:
use Symfony\Component\Console\{
Helper\Table,
Input\ArrayInput,
Input\InputInterface,
Output\NullOutput,
Output\OutputInterface,
Question\Question,
Question\ChoiceQuestion as Choice,
Question\ConfirmationQuestion,
};
但出于某种原因,相同的语法不适用于单词名称空间(所有名称都来自相同的 global
名称空间,如 manual 所要求),如下所示:
use {ArrayAccess, Closure, Countable, IteratorAggregate};
//or
use \{ArrayAccess, Closure, Countable, IteratorAggregate};
两者都给出了错误(而 IDE 没有显示任何语法问题):
PHP Parse error: syntax error, unexpected '{', expecting identifier (T_STRING) or function (T_FUNCTION) or const (T_CONST) or \ (T_NS_SEPARATOR) in ...
多个命名空间的简单标准use
按预期工作:
use ArrayAccess, Closure, Countable, IteratorAggregate; //no errors
那么有什么理由不能在这里应用这样的语法吗?
1) 正如 "Stefan W" 在 PHP: Traits - Manual 上写的评论:
"use" for namespaces always sees its arguments as absolute (starting at the global namespace):
这就是您的示例没有错误的原因:
use ArrayAccess, Closure, Countable, IteratorAggregate; //no errors
第二部分是,如果我们阅读 PHP namespaces specification,我们可以看到您使用了以下有效模式:
use namespace-use-clauses ;
或者如果我们阅读 "Zend language parser",我们可以看到同样的事情:
| T_USE use_declarations ';'
其中 T_USE 是我们 php 代码中的 "use" 关键字,而 use_declarations 是 1 个或多个 use_declaration 元素(基本上是命名空间名称,您将在下面看到)的列表,用逗号分隔。
有趣的是,如果我们像这样重写上面的例子:
use \ArrayAccess, \Closure, \Countable, \IteratorAggregate;
它也会起作用!我们可以看到这个模式 here in the specification 并且我们实际上在 Zend 语言解析器中有这个模式,因为每个 use_declaration 元素匹配以下模式:
use_declaration:
unprefixed_use_declaration
| T_NS_SEPARATOR unprefixed_use_declaration
;
其中 T_NS_SEPARATOR 是反斜杠 - "\".
2) group use 声明呢
嗯,如果这个例子没问题的话:
use Symfony\Component\Console\{Helper\Table, Input\ArrayInput, Input\InputInterface, Output\NullOutput, Output\OutputInterface, Question\Question, Question\ChoiceQuestion as Choice, Question\ConfirmationQuestion};
为什么我们不能写这样的东西?:
use {ArrayAccess, Closure, Countable, IteratorAggregate};
//or
use \{ArrayAccess, Closure, Countable, IteratorAggregate};
回答:那是因为我们没有匹配规范中的任何有效模式,也没有匹配 Zend 语言解析器中的任何模式:
group_use_declaration:
namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations possible_comma '}'
| T_NS_SEPARATOR namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations possible_comma '}'
;
有没有模式如:
'{' unprefixed_use_declarations possible_comma '}'
//or
T_NS_SEPARATOR '{' unprefixed_use_declarations possible_comma '}'
用于团体使用声明。
所以 PHP 7 有很好的命名空间组使用声明,比如 this:
use Symfony\Component\Console\{
Helper\Table,
Input\ArrayInput,
Input\InputInterface,
Output\NullOutput,
Output\OutputInterface,
Question\Question,
Question\ChoiceQuestion as Choice,
Question\ConfirmationQuestion,
};
但出于某种原因,相同的语法不适用于单词名称空间(所有名称都来自相同的 global
名称空间,如 manual 所要求),如下所示:
use {ArrayAccess, Closure, Countable, IteratorAggregate};
//or
use \{ArrayAccess, Closure, Countable, IteratorAggregate};
两者都给出了错误(而 IDE 没有显示任何语法问题):
PHP Parse error: syntax error, unexpected '{', expecting identifier (T_STRING) or function (T_FUNCTION) or const (T_CONST) or \ (T_NS_SEPARATOR) in ...
多个命名空间的简单标准use
按预期工作:
use ArrayAccess, Closure, Countable, IteratorAggregate; //no errors
那么有什么理由不能在这里应用这样的语法吗?
1) 正如 "Stefan W" 在 PHP: Traits - Manual 上写的评论:
"use" for namespaces always sees its arguments as absolute (starting at the global namespace):
这就是您的示例没有错误的原因:
use ArrayAccess, Closure, Countable, IteratorAggregate; //no errors
第二部分是,如果我们阅读 PHP namespaces specification,我们可以看到您使用了以下有效模式:
use namespace-use-clauses ;
或者如果我们阅读 "Zend language parser",我们可以看到同样的事情:
| T_USE use_declarations ';'
其中 T_USE 是我们 php 代码中的 "use" 关键字,而 use_declarations 是 1 个或多个 use_declaration 元素(基本上是命名空间名称,您将在下面看到)的列表,用逗号分隔。
有趣的是,如果我们像这样重写上面的例子:
use \ArrayAccess, \Closure, \Countable, \IteratorAggregate;
它也会起作用!我们可以看到这个模式 here in the specification 并且我们实际上在 Zend 语言解析器中有这个模式,因为每个 use_declaration 元素匹配以下模式:
use_declaration:
unprefixed_use_declaration
| T_NS_SEPARATOR unprefixed_use_declaration
;
其中 T_NS_SEPARATOR 是反斜杠 - "\".
2) group use 声明呢
嗯,如果这个例子没问题的话:
use Symfony\Component\Console\{Helper\Table, Input\ArrayInput, Input\InputInterface, Output\NullOutput, Output\OutputInterface, Question\Question, Question\ChoiceQuestion as Choice, Question\ConfirmationQuestion};
为什么我们不能写这样的东西?:
use {ArrayAccess, Closure, Countable, IteratorAggregate};
//or
use \{ArrayAccess, Closure, Countable, IteratorAggregate};
回答:那是因为我们没有匹配规范中的任何有效模式,也没有匹配 Zend 语言解析器中的任何模式:
group_use_declaration:
namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations possible_comma '}'
| T_NS_SEPARATOR namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations possible_comma '}'
;
有没有模式如:
'{' unprefixed_use_declarations possible_comma '}'
//or
T_NS_SEPARATOR '{' unprefixed_use_declarations possible_comma '}'
用于团体使用声明。