在Symfony2中,\ DateTime是什么意思?

时间:2022-06-16 07:17:43

In Symfony 2, what does this line mean:

在Symfony 2中,这一行意味着什么:

$task->setDueDate(new \DateTime('tomorrow'));

what does \DateTime signify? Can it be accessed from anywhere?

什么\日期时间表示什么?可以从任何地方访问吗?

4 个解决方案

#1


34  

A small FYI firstly, this doesn't have anything to do with Symfony - it just happens that Symfony2 uses namespaces.

一个小的FYI首先,这与Symfony没有任何关系 - 只是Symfony2使用命名空间。

When not using namespaces, the datetime class is always available through new DateTime() - this is because you're already in the "root" namespace. However, when you're using namespaces, simply using new DateTime() wont work as it will look for that class in the current namespace. Example:

不使用命名空间时,datetime类始终可以通过new DateTime()获得 - 这是因为您已经在“root”命名空间中。但是,当您使用名称空间时,只使用新的DateTime()不会起作用,因为它将在当前名称空间中查找该类。例:

<?php

namespace MyApp\Component;

class Something
{
   function __construct()
   {
      $dt = new DateTime(); 
   }
}

This will cause an error (e.g. Class 'MyApp\Component\DateTime' not found in ...), because there is no class within the MyApp\Component namespace named DateTime.

这将导致错误(例如,在...中找不到类'MyApp \ Component \ DateTime'),因为MyApp \ Component命名空间中没有名为DateTime的类。

This is why you found \DateTime(), its telling the interpreter to look in the "root" (?) namespace for the class DateTime.

这就是为什么你找到\ DateTime(),它告诉解释器查看类DateTime的“root”(?)命名空间。

You can also use the use keyword to import the DateTime class - the top of your script would look like - this allows you to just call new DateTime():

您还可以使用use关键字导入DateTime类 - 脚本的顶部看起来像 - 这允许您只调用new DateTime():

<?php

namespace MyApp\Component;

use \DateTime;

#2


3  

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

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

You should also see a namespace X\Y; at the top of the file, \DateTime means that the class DateTime is to be taken from the global namespace, instead of X\Y.

您还应该看到名称空间X \ Y;在文件的顶部,\ DateTime意味着将从全局命名空间而不是X \ Y中获取类DateTime。

i.e this DateTime.

即这个DateTime。

#3


2  

As other said, it referenced to global namespace, and DateTime is integrated in php, look here: http://www.php.net/manual/en/class.datetime.php so you can use it not only in symfony ;)

正如其他人所说,它引用了全局命名空间,并且DateTime被集成在php中,请看这里:http://www.php.net/manual/en/class.datetime.php所以你不仅可以在symfony中使用它;)

#4


1  

\DateTime is a namespaced DateTime class. There is probably somewhere at the top of the file you're looking at that has a use \X\X deceleration. \DateTime should be accessible anywhere you can declare use \X\X

\ DateTime是一个命名空间的DateTime类。可能在文件顶部的某个位置有一个使用\ X \ X减速。 \ DateTime应该可以在任何可以声明使用\ X \ X的地方访问

#1


34  

A small FYI firstly, this doesn't have anything to do with Symfony - it just happens that Symfony2 uses namespaces.

一个小的FYI首先,这与Symfony没有任何关系 - 只是Symfony2使用命名空间。

When not using namespaces, the datetime class is always available through new DateTime() - this is because you're already in the "root" namespace. However, when you're using namespaces, simply using new DateTime() wont work as it will look for that class in the current namespace. Example:

不使用命名空间时,datetime类始终可以通过new DateTime()获得 - 这是因为您已经在“root”命名空间中。但是,当您使用名称空间时,只使用新的DateTime()不会起作用,因为它将在当前名称空间中查找该类。例:

<?php

namespace MyApp\Component;

class Something
{
   function __construct()
   {
      $dt = new DateTime(); 
   }
}

This will cause an error (e.g. Class 'MyApp\Component\DateTime' not found in ...), because there is no class within the MyApp\Component namespace named DateTime.

这将导致错误(例如,在...中找不到类'MyApp \ Component \ DateTime'),因为MyApp \ Component命名空间中没有名为DateTime的类。

This is why you found \DateTime(), its telling the interpreter to look in the "root" (?) namespace for the class DateTime.

这就是为什么你找到\ DateTime(),它告诉解释器查看类DateTime的“root”(?)命名空间。

You can also use the use keyword to import the DateTime class - the top of your script would look like - this allows you to just call new DateTime():

您还可以使用use关键字导入DateTime类 - 脚本的顶部看起来像 - 这允许您只调用new DateTime():

<?php

namespace MyApp\Component;

use \DateTime;

#2


3  

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

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

You should also see a namespace X\Y; at the top of the file, \DateTime means that the class DateTime is to be taken from the global namespace, instead of X\Y.

您还应该看到名称空间X \ Y;在文件的顶部,\ DateTime意味着将从全局命名空间而不是X \ Y中获取类DateTime。

i.e this DateTime.

即这个DateTime。

#3


2  

As other said, it referenced to global namespace, and DateTime is integrated in php, look here: http://www.php.net/manual/en/class.datetime.php so you can use it not only in symfony ;)

正如其他人所说,它引用了全局命名空间,并且DateTime被集成在php中,请看这里:http://www.php.net/manual/en/class.datetime.php所以你不仅可以在symfony中使用它;)

#4


1  

\DateTime is a namespaced DateTime class. There is probably somewhere at the top of the file you're looking at that has a use \X\X deceleration. \DateTime should be accessible anywhere you can declare use \X\X

\ DateTime是一个命名空间的DateTime类。可能在文件顶部的某个位置有一个使用\ X \ X减速。 \ DateTime应该可以在任何可以声明使用\ X \ X的地方访问