PHP Class DateTime not found

2020-06-30 05:10发布

问题:

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:

  1. 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.

  2. 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?

回答1:

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;


回答2:

I guess You're using namespaces and trying to use DateTime (relative to the current namespace) instead of \DateTime (the full qulified class name)