奇怪的PHP自动加载问题 [英] Strange PHP autoload issue

查看:112
本文介绍了奇怪的PHP自动加载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些方法看起来像

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');
    }
}

如你所知,DateTime是内置类PHP。问题是,脚本尝试从classes文件夹加载它。

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文件夹的路径> /class.DateTime.php

在这种情况下我能做些什么?

What can I do in this case?

推荐答案

可能是你的方法'getTime()'试图解析当前命名空间中的DateTime类。

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

如果你想使用内置的DateTime类,你必须参考全局命名空间

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

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

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

这篇关于奇怪的PHP自动加载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆