1

OK, so, I've been using auto-loaders for a while, become rather used to MVC, and decided to try to tackle namespaces. Well, this hasn't worked as expected. I have managed to reproduce the error in a very simple way, so the contents of my php file read:

<?php
namespace site\test;

echo 'hello';

The output is not what I expected:

Fatal error: Namespace declaration statement has to be the very first statement in the script in file on line 2

I'm running PHP 5.3.10 on a Synology NAS, using the latest version of DSM (Synology's OS). This makes use of Apache v2.2.22. I can confirm that commenting out the "namespace" line returns the word "hello", with no extra characters before it.

There is no BOM at the beginning of the file. I also have no issues with setting headers such as "Location" which would suggest this as being an issue.

Line endings are Unix (\n only) and the character encoding of the file is UTF8.

The PHP setting "auto_prepend_file" is empty.

4

3 回答 3

2

命名空间前有空格吗?命名空间必须出现在任何输出之前。这包括浏览器的 HTML。这还包括一个 UTF-8 字节顺序标记 (BOM),它可以从某些编辑器中爬进来。

尝试在 Linux 等编辑器中打开文件,vi它应该会显示开头是否有 BOM。

检查如何修复 PHP 中的“标头已发送”错误,以获取有关在文件中开始输出的其他方式的信息。

于 2013-02-13T21:22:35.287 回答
1

当您有一个以 UTF-8 BOM作为第一个字符的文件并且您应该删除开始标记之前的所有空格时,就会发生这种情况。

于 2013-02-13T21:23:19.480 回答
0

重新安装 DSM(Synology OS)实际上解决了这个问题。完成后,从其他框架获得的一些类在文件开头有“BOM”字节,但不是全部。BOM 问题的简单解决方案是将“BOM”移除器添加到自动加载器中。

于 2013-02-16T22:50:25.103 回答