6

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.

Webeng
  • 7,050
  • 4
  • 31
  • 59
  • Look at http://stackoverflow.com/questions/19699319/php-namespace-pdo-not-found probably falls under a possible duplicate also. – Funk Forty Niner May 23 '16 at 00:24
  • @Fred-ii- If it is a duplicate you can place it as one if you want. I however wasn't able to find my specific answer there. Yes the users solution to his problem was to place a backslash when using a namespace, though I'm confused as to what the purpose is with the backslash. I have had a few situations in the past where I had code that didn't work, and after placing a backslash it would work magically, but I never understood why. – Webeng May 23 '16 at 00:35
  • As Mario stated in a comment below an answer given (whether that answer will still exist) has to do with namespacing http://php.net/manual/en/language.namespaces.php (something I also thought of). – Funk Forty Niner May 23 '16 at 00:52
  • This one http://stackoverflow.com/questions/3384204/what-are-namespaces may be more fitting as a possible duplicate – Funk Forty Niner May 23 '16 at 00:56
  • 3
    If you have `namespace` at the top of the file e,g, `namespace myApp;` then any class that isn't specifically `namespaced` when used in the code will get the get the current namespace 'prepended' to it. So, `new PDO();` would become: `new MyApp\PDO();` which doesn't exist and will fail. The 'root' namespace is called: **'\'**. So you need to use `new \PDO();` to prevent the automatic namespace completion. – Ryan Vincent May 23 '16 at 01:16
  • @RyanVincent Dude that answer is legit. If you post the answer to this question I'll give it the green checkmark. Just one more thing, then namespaces use backslashes instead of forward slash as their way of locating the "path" of the class? (if it can be considered a path) – Webeng May 23 '16 at 01:26
  • 1
    Yes, backslashes ('\') are used as the namespace path separators. It is all explained in the links mentioned in the previous comments. Also, maybe interesting? [A Brief Introduction to PHP Namespacing](https://mattstauffer.co/blog/a-brief-introduction-to-php-namespacing). There are standards about it as well - http://www.php-fig.org/psr/psr-4/ – Ryan Vincent May 23 '16 at 01:45
  • 1
    Oh boy, this is a bogus duplicate. The duplicate doesn't resolve relative and absolute paths to namespaces in _why_ a global class like `PDO` or a global function like `strlen` needs to be set to the absolute path `\\` in order to be recognized. It doesn't resolve global and local scope nor anything else useful that hasn't been written in the [manual](http://php.net/manual/en/language.namespaces.basics.php) This question is legit and asks WHY a backslash has to be used, not how namespaces work, the OP obviously grasps 80% of the theory. – dbf May 23 '16 at 08:38

2 Answers2

-1

Backslash sometimes are used within a framework controller or model like ZF2, Laravel to specify that you want you use a PHP native class not a Framework class

-1

From what I understand it has to do with where in your file structure your class declarations are. If you are working within a folder for custom classes, say ones dedicated to a Guestbook and you're in the Guestbook folder you may want to access a class you have that you'll use frequently, in many cases a database handler like PDO or MySQLi. These would be stored in a root directory, so you'd want to tell PHP where to look for the file.

So, let's say your file structure is similar to this:

Root - Guestbook - Forum - Store items

You're working on the Store items directory, but want to access the PDO handler in your root directory, so you'd access it by declaring it: $conn = new \PDO;

Likewise, let's say you're in the Forum section and want to pull in information from the Store, you'd just access it like this: $item = new \Store\StoreItem;

At least...that's what I think it is! If you're working with all your classes declared locally you don't need to worry about directory structure so much.

aylnon
  • 371
  • 2
  • 8