问题
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