问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/26346705/why-do-some-php-programmers-use-a-double-backslash-in-their-namespaces-instead-o