1

What's the purpose of using backslash \ when we create an object in PHP?

$iter = new \ArrayIterator($arr);

EM-Creations
  • 4,195
  • 4
  • 40
  • 56
Turn
  • 45
  • 1
  • 9

1 Answers1

1

It's used to create a new object of a fully qualified class. Say, you're code is in the namespace "Namespace1":

namespace Namespace1;
$iter = new ArrayIterator();

would be resolved as Namespace1\ArrayIterator(); and

$iter = new \ArrayIterator();

would be resolved as ArrayIterator();

See: http://php.net/manual/de/language.namespaces.basics.php for more infos about namespaces.

Florian
  • 2,796
  • 1
  • 15
  • 25