[英]What does a \ (backslash) do in PHP (5.3+)?
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);
}
}
命名空間
在 PHP 5.3+ 中,反斜杠\\
符號用於命名空間。 它是指示命名空間的開始符號,也用作子命名空間名稱之間的分隔符。
請參閱有關命名空間的官方文檔。
緩存
此外,在 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"
澄清潛在的混淆:
反斜杠並不意味着類繼承。
在下面, Animal
, Dog
, Shepherd
不必是類,而只是namespaces 。 意思是用於將名稱組合在一起以避免命名沖突的東西。
$myDog = new \Animal\Dog\Shepherd\GermanShepherd();
領先的\\
表示Animal
是在全局范圍內聲明的。
\\
在 PHP 5.3 中用於命名空間。 有關名稱空間和 PHP 的更多信息,請參閱http://www.php.net/manual/en/language.namespaces.rationale.php 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.