为什么有些 PHP 程序员在他们的命名空间中使用双反斜杠而不是一个? [英] Why do some PHP programmers use a double backslash in their namespaces instead of a single one?
问题描述
今天在扫描一些库代码时,我已经看到好几次了:不是声明一个 Namespace\Like\This
,而是 Done\\Like\\This
.
While scanning some library code today, I've seen it several times: Instead of declaring a Namespace\Like\This
, it's Done\\Like\\This
.
有人能指教我 - 这背后的原因是什么?
Could somebody please enlighten me – whats the reason behind this?
我只能想象它要么是特定于框架的东西(我不相信),要么是我不理解的一种奇怪的转义形式.
All I can images that it's either something framework specific (which I don't believe), or it's a strange form of escaping that I don't understand.
推荐答案
当你在字符串中引用类名时,你应该使用双反斜杠,例如:$className = "Foo\\Bar".
这是防止字符转义,一个熟悉的例子是换行符 echo "Hello World\n";
如果你有两个反斜杠,它会打印 "Hello World\n" 而不是 "Hello World"换行.
You should use double backslash when you're referencing the class name in a string, eg: $className = "Foo\\Bar".
This is prevent the escaping of characters, a familiar example would be a newline echo "Hello World\n";
If you had two backslashes it would print "Hello World\n" rather than "Hello World" with a new line.
这篇关于为什么有些 PHP 程序员在他们的命名空间中使用双反斜杠而不是一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!