1

Within some PHP code, I see the following ...

...
$sg = new \SendGrid(
    ...
);
$email = new \SendGrid\Email();
...

I understand that the backslash is usually to escape special characters... but here what is the backslash for? What is it doing?

Grateful
  • 9,685
  • 10
  • 45
  • 77
  • Possible duplicate of [Backslash syntax when creating objects](http://stackoverflow.com/questions/4075521/backslash-syntax-when-creating-objects) – giusti Jan 03 '17 at 02:18

2 Answers2

1

This is to access another namespace in PHP. in your case, you are trying to access the class Email from the namespace SendGrid. You don't need to really understand that concept to make your code works. Also, before asking question on SO, you should try to do some research. Don't mean to be rude but this is a pretty low quality question. Hope it helps !

  • Nic
Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • Thank you for your comments. I did try to do some research, but as a PHP newbie... perhaps I was looking in the wrong areas. – Grateful Jan 03 '17 at 02:11
1

\ (backslash) is the namespace separator in PHP 5.3.

A \ before the beginning of a function represents the Global Namespace.

Putting it there will ensure that the function called is from the global namespace, even if there is a function by the same name in the current namespace.

[ Reference: What does a \ (backslash) do in PHP (5.3+)? ]

Community
  • 1
  • 1
Grateful
  • 9,685
  • 10
  • 45
  • 77