未找到 PHP 类 DateTime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10841960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Class DateTime not found
提问by ElPiter
Missing something when declaring a DateTime object in PHP 5.3.8
在PHP 5.3.8 中声明 DateTime 对象时遗漏了一些东西
I get a JSON string with a determinate date time which is passed to my php controller.
我得到一个带有确定日期时间的 JSON 字符串,该字符串被传递给我的 php 控制器。
For some reason, I am not getting it to be mapped as a DateTime object in php. But sort of weirdly. See the following images:
出于某种原因,我没有将它映射为 php 中的 DateTime 对象。但有点奇怪。请参阅以下图片:
As you can see in the Expressions Window (top right), BEFORE the step, I am checking that new DateTime(myVariable) is bringing and transforming correctly what I need. In the first watch, the variable to pass to DateTime constructor. In the second watch, the expression newDateTime(myVariable) already mapped as a DateTimeObject. Apparently fine up to here.

But, sadly, when I go forward and press F6, the following exemption (see also the image below) is thrown:
Fatal error: Class 'Acme\StoreBundle\Repository\DateTime' not found in /Users/pgbonino/Sites/Symfony/src/Acme/StoreBundle/Repository/HistoryRepository.php on line 19 Call Stack: 0.0201 693568 1. {main}() /Users/pgbonino/Sites/Symfony/web/app_dev.php:0 0.0267 2106576 2. Symfony\Component\HttpKernel\Kernel->handle(???, ???, ???) /Users/pgbonino/Sites/Symfony/web/app_dev.php:24 0.0377 2649176 3. Symfony\Bundle\FrameworkBundle\HttpKernel->handle(???, ???, ???) /Users/pgbonino/Sites/Symfony/app/bootstrap.php.cache:547 0.0378 2650832 4. Symfony\Component\HttpKernel\HttpKernel->handle(???, ???, ???) /Users/pgbonino/Sites/Symfony/app/cache/dev/classes.php:4879 0.0378 2650832 5. Symfony\Component\HttpKernel\HttpKernel->handleRaw(???, ???) /Users/pgbonino/Sites/Symfony/app/cache/dev/classes.php:3875 0.1574 5562232 6. call_user_func_array(???, ???) /Users/pgbonino/Sites/Symfony/app/cache/dev/classes.php:3905 0.1574 5562600 7. Acme\StoreBundle\Controller\HistoryController->saveTestAction() /Users/pgbonino/Sites/Symfony/app/cache/dev/classes.php:3905 0.1694 5739032 8. Acme\StoreBundle\Repository\HistoryRepository->saveTestInHistory(???, ???) /Users/pgbonino/Sites/Symfony/src/Acme/StoreBundle/Controller/HistoryController.php:33
正如您在表达式窗口(右上角)中看到的那样,在此步骤之前,我正在检查 new DateTime(myVariable) 是否正确地带来并转换了我需要的内容。在第一个 watch 中,传递给 DateTime 构造函数的变量。在第二个监视中,表达式 newDateTime(myVariable) 已经映射为 DateTimeObject。到这里显然还好。

但是,遗憾的是,当我继续按 F6 时,会抛出以下豁免(另请参见下图):
Fatal error: Class 'Acme\StoreBundle\Repository\DateTime' not found in /Users/pgbonino/Sites/Symfony/src/Acme/StoreBundle/Repository/HistoryRepository.php on line 19 Call Stack: 0.0201 693568 1. {main}() /Users/pgbonino/Sites/Symfony/web/app_dev.php:0 0.0267 2106576 2. Symfony\Component\HttpKernel\Kernel->handle(???, ???, ???) /Users/pgbonino/Sites/Symfony/web/app_dev.php:24 0.0377 2649176 3. Symfony\Bundle\FrameworkBundle\HttpKernel->handle(???, ???, ???) /Users/pgbonino/Sites/Symfony/app/bootstrap.php.cache:547 0.0378 2650832 4. Symfony\Component\HttpKernel\HttpKernel->handle(???, ???, ???) /Users/pgbonino/Sites/Symfony/app/cache/dev/classes.php:4879 0.0378 2650832 5. Symfony\Component\HttpKernel\HttpKernel->handleRaw(???, ???) /Users/pgbonino/Sites/Symfony/app/cache/dev/classes.php:3875 0.1574 5562232 6. call_user_func_array(???, ???) /Users/pgbonino/Sites/Symfony/app/cache/dev/classes.php:3905 0.1574 5562600 7. Acme\StoreBundle\Controller\HistoryController->saveTestAction() /Users/pgbonino/Sites/Symfony/app/cache/dev/classes.php:3905 0.1694 5739032 8. Acme\StoreBundle\Repository\HistoryRepository->saveTestInHistory(???, ???) /Users/pgbonino/Sites/Symfony/src/Acme/StoreBundle/Controller/HistoryController.php:33
So, strangely, the Watch Expressions window from Eclipse is not working just like the execution engine and/or vice-versa.
因此,奇怪的是,Eclipse 的 Watch Expressions 窗口不像执行引擎那样工作,反之亦然。
Of course, I'd prefer it to be the opposite (It worked in the execution and not in the watch window :) ).
当然,我更希望它是相反的(它在执行中起作用,而不是在监视窗口中起作用 :))。
So, any idea?
那么,有什么想法吗?
回答by zerkms
You're currently in the Acme\StoreBundle\Repository\DateTimenapespace. To address to the default namespace in this case you need to put leading \before your classname, like
你目前在Acme\StoreBundle\Repository\DateTime后宫。在这种情况下,要寻址到默认命名空间,您需要\在类名之前放置前导,例如
$dt = new \DateTime(...);
so
所以
namespace foo;
$obj = new class();
will try to find classdefinition within foonamespace.
将尝试class在foo命名空间中找到定义。
And
和
namespace foo;
$obj = new \class();
will try to find classdefinition within global namespace.
将尝试class在全局命名空间中找到定义。
As an alternative you could import the class using
作为替代方案,您可以使用
use \DateTime;
or create alias (in case if you already have the class with the same name in current NS):
或创建别名(如果您在当前 NS 中已经有同名的类):
use \DateTime as NewDT;
回答by marcelog
I guess You're using namespaces and trying to use DateTime (relative to the current namespace) instead of \DateTime (the full qulified class name)
我猜您正在使用命名空间并尝试使用 DateTime(相对于当前命名空间)而不是 \DateTime(完整的合格类名)

