命名空间自动加载可在Windows下运行,但不能在Linux上运行 [英] Namespace Autoload works under windows, but not on Linux
问题描述
我有以下php代码:
index.php
<?php
spl_autoload_extensions(".php");
spl_autoload_register();
use modules\standard as std;
$handler = new std\handler();
$handler->delegate();
?>
modules \ standard \ handler.php
<?php
namespace modules\standard {
class handler {
function delegate(){
echo 'Hello from delegation!';
}
}
}
?>
在Windows 7中,运行WAMP的代码将生成消息来自委托的问候!".但是在Linux下,我得到以下信息:
Under Windows 7, running WAMP, the code produces the message "Hello from Delegation!" however under Linux, I get the following:
致命错误:spl_autoload():无法在第15行的/var/www/index.php中加载类模块\ standard \ handler
Fatal error: spl_autoload(): Class modules\standard\handler could not be loaded in /var/www/index.php on line 15
Windows在WAMP下运行PHP 5.3.0,而Linux在Ubuntu 9.10下运行5.3.2 dotdeb软件包.
Windows is running PHP 5.3.0 under WAMP, and Linux is running the 5.3.2 dotdeb package under Ubuntu 9.10.
这是我的Linux机器上的配置问题,还是只是在不同操作系统上处理名称空间和自动加载方式的差异
Is this a configuration issue on my linux box, or just a difference in the way namespaces and autoloading is handled on the different operating systems
推荐答案
SPL自动加载器非常原始-它不知道名称空间,因此在Linux/Unix上,它尝试使用名称\加载文件.分隔符是/不是.
The SPL autoloader is extremely primitive - it has no knowledge of namespaces, so it tries to load a file with \ in it's name while on Linux/Unix the path separator is / not .
这篇关于命名空间自动加载可在Windows下运行,但不能在Linux上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!