\(反斜杠)在PHP(5.3+;)中起什么作用?

\(反斜杠)在PHP(5.3+;)中起什么作用?,php,namespaces,opcode,opcache,Php,Namespaces,Opcode,Opcache,\在PHP中做什么 例如,has\FALSE、\session\u id和\Exception: public function __construct($timeout=300, $acceptGet=\FALSE){ $this->timeout = $timeout; if (\session_id()) { $this->acceptGet = (bool) $acceptGet; } else { throw new

\
在PHP中做什么

例如,has
\FALSE
\session\u 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);
    }
}
\
(反斜杠)是PHP5.3中的名称空间分隔符

函数开头之前的
\
表示


将其放在那里将确保调用的函数来自全局名称空间,即使当前名称空间中存在同名函数。

PHP 5.3中使用了
\
名称空间。有关名称空间和PHP的更多信息,请参阅。

以澄清潜在的混淆:

反斜杠并不意味着类继承

在下面的例子中,
Animal
Dog
Shepherd
不必是类,只需简单。意思是用来把名字组合在一起的东西


领先的
\
意味着
动物
已在全球范围内宣布。

名称空间

在PHP5.3+中,反斜杠
\
符号用于名称空间。它是指示名称空间的开始符号,同时也是子名称空间名称之间的分隔符

请参阅有关的官方文档

Opcache

此外,在PHP7.0+中,OPCache将一些函数替换为操作码,这使得这些特定函数运行得更快。但是,这仅在将函数放置在根命名空间中时有效。有关此主题,请参见此。因此,除了名称空间之外,
\
还间接影响代码优化

以下本机函数受益于此效果:

"array_slice"
"assert"
"boolval"
"call_user_func"
"call_user_func_array"
"chr"
"count"
"defined"
"doubleval"
"floatval"
"func_get_args"
"func_num_args"
"get_called_class"
"get_class"
"gettype"
"in_array"
"intval"
"is_array"
"is_bool"
"is_double"
"is_float"
"is_int"
"is_integer"
"is_long"
"is_null"
"is_object"
"is_real"
"is_resource"
"is_string"
"ord"
"strlen"
"strval"

@马里奥非常好。谢谢你的链接:)我想这个问题应该结束了,但我自己无法结束主题。这是一个重复的问题,但不是一个坏问题。但是你可以在标题上加上“反斜杠”,这样别人就更容易在谷歌上搜索了。重要的是要意识到,他们不再向messenger开枪了。当你发现它是什么时,不要责怪我们:)哦,不,我太晚了。如果我的服务器导致错误,那么删除它安全吗?PhpUnit不喜欢使用它:)\var\u export()if(\defined('PhpUnit\u COMPOSER\u INSTALL')){;}但是为什么在
FALSE之前有反斜杠呢?如果它导致代码运行得更快,为什么默认情况下不应用它?谁不喜欢性能?可能是因为OPCache在很长一段时间内没有默认配置,而且该功能直到第7版才被包括在内。它还需要知识来建立和管理。
"array_slice"
"assert"
"boolval"
"call_user_func"
"call_user_func_array"
"chr"
"count"
"defined"
"doubleval"
"floatval"
"func_get_args"
"func_num_args"
"get_called_class"
"get_class"
"gettype"
"in_array"
"intval"
"is_array"
"is_bool"
"is_double"
"is_float"
"is_int"
"is_integer"
"is_long"
"is_null"
"is_object"
"is_real"
"is_resource"
"is_string"
"ord"
"strlen"
"strval"