Why do some PHP programmers use a double backslash in their namespaces instead of a single one?

*爱你&永不变心* 提交于 2021-02-04 14:18:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!