0

Possible Duplicate:
Backslash syntax when creating objects
Backslash in PHP — what does it mean?

Can someone explain me what is the meaning of this "\" in this code snippet

throw new \RuntimeException("Unable to cache the data with ID '$id'.");

as opposed to

throw new RuntimeException("Unable to cache the data with ID '$id'.");
Community
  • 1
  • 1
mahen3d
  • 7,047
  • 13
  • 51
  • 103

3 Answers3

4

When using backslashes between classes names, you are specifying the namespace. Namespaces where adopted im PHP since the version 5.3. A namespace is a logical identifier to group related classes.

The backslash here:

throw new \RuntimeException("Unable to cache the data with ID '$id'.");

means that PHP 5.3 will try to find the class RuntimeException on the current working directory and on every included path. Backslash means absolute path. Other wise when not passing the backslash is a relative path to the class.

This is the same as in the directories path.

slash28cu
  • 1,614
  • 11
  • 23
2

It's a backslash, and it's to do with namespaces.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1
throw new \RuntimeException("Unable to cache the data with ID '$id'.");

In this scenario, without having any namespace defined, it's simply the samething that not having the .

But, it's also used to access internal or global classes of a namespace.

When used with a string or multiple string\ it's to access the specific class from a specific namespace.

Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341