Symfony: why the backslash in \ DateTime ();

Why does this line of symfony code have a backslash before DateTime()?

$this->updated_datetime = new \DateTime();

I'm sure this has something to do with NameSpaces, but can someone confirm / clarify ...

+4
source share
1 answer

Because it makes php refer to the root (global) namespace in this way.

You can also use DateTime and then jump without a slash:

namespace MyCompany\MyBundle\MyController;

use \DateTime;

$d = new DateTime();

Say you are working on your controller, which is under the namespace MyCompany \ MyBundle \ MyController . So what happens when you try to create a new DateTime instance?

, MyCompany\MyBundle\MyController\DateTime. " ...".

- php , .

: http://php.net/manual/en/language.namespaces.global.php

+9

Source: https://habr.com/ru/post/1665643/


All Articles