A (Backslash)在PHP(5.3+)中做什么?
-
24-10-2019 - |
题
什么是 \
在PHP中做?
例如, CSRF4PHP 有 \FALSE
, \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);
}
}
解决方案
\
(Backslash)是PHP 5.3中的名称空间分离器。
一个 \
在函数开始之前代表 全局名称空间.
将其放置在此处确保所调用的函数来自全局名称空间,即使当前名称空间中的同名函数也有相同的函数。
其他提示
名称空间
在php 5.3+中 \
符号用于名称空间。它是指示名称空间的开始符号,并且还用作子名称名称之间的分离器。
请参阅有关的官方文件名称领域.
opcache
此外,在PHP 7.0+中,一些功能是 替换为opcodes 经过 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"
这 \
用于名称空间中的PHP 5.3中。看 http://www.php.net/manual/en/language.namespaces.rationale.php 有关名称空间和PHP的更多信息。
不隶属于 StackOverflow