I have seen a lot of code that includes a backslash before the class type in the initiation of a new object:
db = new \PDO($dsn, $username, $password);
QUESTION:
What is the purpose of the backslash in the previous code?
My Inferences:
I have often used db = new PDO(...)
in my code without the backslash to establish a connection with MySQL and it has worked fine for me, though I have often seen the backslash in the code of other users. At first glance it seemed to be specifying the path of a class, though that seemed odd to me since I usually use a forward slash /
for paths in my code, and also because I have never read or seen that to be a requirement when initiating an object of any class type.
Considering that the code has worked for me without a backslash, I am puzzled with what the purpose of \
might be.
I have tried to find documentation on it without success (http://php.net/manual/en/language.oop5.php) though I'm sure it's out there.