Strange PHP autoload issue

无人久伴 提交于 2019-12-10 17:41:31

问题


I have some method which looks like that

public function getTime() {
    $date = new DateTime();
    $date->setTimezone(new DateTimeZone('Europe/Paris'));
    return $date->format('Y-m-d H:i:s');
}

Calling this method from inside file which has autoload function

function __autoload($class_name) {
    global $path;
    if (file_exists($path['classes'] . ds  . 'class.'. $class_name . '.php')) {
        require_once($path['classes'] . ds . 'class.'. $class_name . '.php');
    } else {
        die($path['classes'] . ds . 'class.'.$class_name . '.php');
    }
}

As you know, DateTime is in-built class of PHP. The problem is, script tries to load it from classes folder. This method works in my local server but remote webserver dies with following return.

<path to classes folder>/class.DateTime.php

What can I do in this case?


回答1:


It may be the case that your method getTime() is trying to resolve a DateTime class in your current namespace.

If you wish to use the in-built DateTime class you will have to refer to the global namespace.

From this:

public function getTime() {
    $date = new DateTime();
    $date->setTimezone(new DateTimeZone('Europe/Paris'));
    return $date->format('Y-m-d H:i:s');
}

To this:

public function getTime() {
    $date = new \DateTime();
    $date->setTimezone(new \DateTimeZone('Europe/Paris'));
    return $date->format('Y-m-d H:i:s');
}


来源:https://stackoverflow.com/questions/9539422/strange-php-autoload-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!