函数名称前面的 \(反斜杠)是什么意思?
\
在 PHP 中起什么作用?
例如, CSRF4PHP 具有 \FALSE< /code>、
\session_id
和 \Exception
:
public function __construct($timeout=300, $acceptGet=\FALSE){
$this->timeout = $timeout;
if (\session_id()) {
$this->acceptGet = (bool) $acceptGet;
} else {
throw new \Exception('Could not find session id', 1);
}
}
What does a \
do in PHP?
For example, CSRF4PHP has \FALSE
, \session_id
, and \Exception
:
public function __construct($timeout=300, $acceptGet=\FALSE){
$this->timeout = $timeout;
if (\session_id()) {
$this->acceptGet = (bool) $acceptGet;
} else {
throw new \Exception('Could not find session id', 1);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
\
(反斜杠)是 PHP 5.3 中的命名空间分隔符。函数开头之前的
\
表示 全局命名空间。将其放在那里将确保调用的函数来自全局命名空间,即使当前命名空间中存在同名函数。
\
(backslash) is the namespace separator in PHP 5.3.A
\
before the beginning of a function represents the Global Namespace.Putting it there will ensure that the function called is from the global namespace, even if there is a function by the same name in the current namespace.
命名空间
在 PHP 5.3+ 中,反斜杠
\
符号用于命名空间。它是指示命名空间的起始符号,也用作子命名空间名称之间的分隔符。请参阅官方文档了解
命名空间。
Opcache
此外,在 PHP 7.0+ 中,一些函数被 OPCache 替换为操作码,这使得这些特定函数的运行速度更快。但是,这仅当函数放置在根命名空间中时才有效。请参阅有关此主题的讨论。所以除了命名空间之外,
\
也会间接影响代码优化。以下本机函数受益于此效果:
Namespaces
In PHP 5.3+ the backslash
\
symbol is used in namespaces. It is the start symbol to indicate a namespace and also serves as a separator between sub-namespace names.See official documentation about
namespacing.
Opcache
Additionally, in PHP 7.0+ some functions are replaced with opcodes by OPCache, which makes these specific functions run a lot faster. However, this only works when the functions are placed in the root namespace. See this discussion about this topic. So besides namespacing, the
\
indirectly also affects code optimisation.The following native functions benefit from this effect:
为了澄清潜在的混乱:
反斜杠并不暗示类继承。
在下面的内容中,
Animal
、Dog
、Shepherd
不必是类,而只需 命名空间。意思是用于将名称分组在一起避免命名冲突。前导
\
表示Animal
是在全局范围内声明的。To clarify potential confusion:
The backslash does not imply class inheritance.
In the following,
Animal
,Dog
,Shepherd
don't have to be classes, but simply namespaces. Meaning something used to group names together to avoid naming collisions.The leading
\
meansAnimal
was declared in the global scope.\
在 PHP 5.3 中用于命名空间。请参阅 http://www.php.net/manual/en/language。 namespaces.rationale.php 了解有关命名空间和 PHP 的更多信息。The
\
is used in PHP 5.3 for namespaces. See http://www.php.net/manual/en/language.namespaces.rationale.php for more information on namespaces and PHP.除了 Webber 的回答之外,以下本机函数也受益于 Opcache 代码优化(参考):
In addition to Webber's answer, below native functions also benefit from Opcache code optimization (reference):