找不到PHP类DateTime [英] PHP Class DateTime not found
问题描述
在 PHP 5.3.8 中声明DateTime对象时缺少某些内容$ p
$ b
我得到了一个带有确定日期时间的JSON字符串,该字符串已传递
出于某种原因,我没有将其映射为php中的DateTime对象。但是有点奇怪。看到以下图像:
-
正如您在表达式窗口(右上方)中看到的那样,在执行此步骤之前,我正在检查新的DateTime(myVariable)正在带来并正确转换我所需要的东西。在第一个监视中,将变量传递给DateTime构造函数。在第二个监视中,表达式newDateTime(myVariable)已映射为DateTimeObject。
-
但是,可悲的是,当我前进并按F6时,会抛出以下豁免项(另请参见下图):
致命错误:在/ Users / pgbonino / Sites / Symfony / src / Acme /中找不到类'Acme\StoreBundle\Repository\DateTime'第19行的StoreBundle / Repository / HistoryRepository.php
调用堆栈:
0.0201 693568 1. {main}()/Users/pgbonino/Sites/Symfony/web/app_dev.php:0
0.0267 2106576 2. Symfony\组件\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\组件\HttpKernel\HttpKernel-> handle(???,???,???)/ Users / pgbonino / Sites / Symfon y / 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
因此,奇怪的是,Eclipse的Watch Expressions窗口不能像执行引擎,反之亦然。
当然,我更喜欢b相反(它在执行中起作用,而不是在监视窗口中起作用:))。
那么,有什么想法吗?
您当前位于 Acme\StoreBundle\Repository\DateTime
napespace中。在这种情况下,要寻址默认名称空间,您需要在类名前加上前导 \
,例如
$ dt = new \DateTime(...);
so
namespace foo;
$ obj = new class();
将尝试在< class
定义中找到 foo
命名空间。
并且
namespace foo;
$ obj = new \class();
将尝试在全局变量中查找 class
的定义
作为替代方案,您可以使用
导入类。使用\DateTime;
或创建别名(以防在当前NS中已有相同名称的类):
使用\DateTime作为NewDT;
Missing something when declaring a DateTime object in PHP 5.3.8
I get a JSON string with a determinate date time which is passed to my php controller.
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:
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
So, strangely, the Watch Expressions window from Eclipse is not working just like the execution engine and/or vice-versa.
Of course, I'd prefer it to be the opposite (It worked in the execution and not in the watch window :) ).
So, any idea?
You're currently in the Acme\StoreBundle\Repository\DateTime
napespace. To address to the default namespace in this case you need to put leading \
before your classname, like
$dt = new \DateTime(...);
so
namespace foo;
$obj = new class();
will try to find class
definition within foo
namespace.
And
namespace foo;
$obj = new \class();
will try to find class
definition within global namespace.
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):
use \DateTime as NewDT;
这篇关于找不到PHP类DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!