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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4790020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:22:31  来源:igfitidea点击:

What does a \ (backslash) do in PHP (5.3+)?

phpnamespacesopcodeopcache

提问by Alfred

What does a \do in PHP?

\PHP中的 a做什么?

For example, CSRF4PHPhas \FALSE, \session_id, and \Exception:

例如,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);
    }
}

回答by Alan Geleynse

\(backslash) is the namespace separator in PHP 5.3.

\(反斜杠)是 PHP 5.3 中的命名空间分隔符。

A \before the beginning of a function represents the Global Namespace.

\函数开头之前的A代表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.

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

回答by Webber

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.

在 PHP 5.3+ 中,反斜杠\符号用于命名空间。它是指示命名空间的开始符号,也用作子命名空间名称之间的分隔符。

See official documentation about namespacing.

请参阅有关命名空间的官方文档 。

Opcache

缓存

Additionally in PHP 7.0+ some functions are replaced with opcodesby 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 discussionabout this topic. So besides namespacing, the \indirectly also affects code optimisation.

此外,在PHP 7.0以上版本的一些功能被替换操作码OPCache,这使得这些特定功能的运行速度快了很多。然而,这只适用于将函数放置在根命名空间中的情况。看到这个讨论这个话题。所以除了命名空间之外,\间接也会影响代码优化。

The following native functions benefit from this effect:

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

"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"

回答by Nelu

To clarify potential confusion:

澄清潜在的混淆:

The backslash does notimply class inheritance.

反斜杠并不意味着类继承

In the following, Animal, Dog, Shepherddon't have to be classes, but simply namespaces. Meaning something used to group names together to avoid naming collisions.

在下面,Animal, Dog,Shepherd不必是类,而只是名称空间。意思是用来将名称组合在一起以避免命名冲突的东西

$myDog = new \Animal\Dog\Shepherd\GermanShepherd();

The leading \means Animalwas declared in the global scope.

领先的\手段Animal是在全局范围内声明的。

回答by Damian Galarza

The \is used in PHP 5.3 for namespaces. See http://www.php.net/manual/en/language.namespaces.rationale.phpfor more information on namespaces and PHP.

\在PHP 5.3用于命名空间。有关名称空间和 PHP 的更多信息,请参阅http://www.php.net/manual/en/language.namespaces.rationale.php