函数名称前面的 \(反斜杠)是什么意思?

发布于 2024-10-13 12:01:01 字数 560 浏览 4 评论 0原文

\ 在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

七禾 2024-10-20 12:01:01

\(反斜杠)是 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.

赠意 2024-10-20 12:01:01

命名空间

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

请参阅官方文档了解
命名空间

Opcache

此外,在 PHP 7.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"
"function_exists"
"is_callable"
"extension_loaded"
"dirname"
"constant"
"define"

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:

"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"
"function_exists"
"is_callable"
"extension_loaded"
"dirname"
"constant"
"define"
回首观望 2024-10-20 12:01:01

为了澄清潜在的混乱:

反斜杠并不暗示类继承

在下面的内容中,AnimalDogShepherd 不必是类,而只需 命名空间。意思是用于将名称分组在一起避免命名冲突

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

前导 \ 表示 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.

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

The leading \ means Animal was declared in the global scope.

拥抱没勇气 2024-10-20 12:01:01

\ 在 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.

尬尬 2024-10-20 12:01:01

除了 Webber 的回答之外,以下本机函数也受益于 Opcache 代码优化(参考):

"function_exists"
"is_callable"
"extension_loaded"
"dirname"
"constant"
"define"

In addition to Webber's answer, below native functions also benefit from Opcache code optimization (reference):

"function_exists"
"is_callable"
"extension_loaded"
"dirname"
"constant"
"define"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文