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.

SEoF
  • 1,092
  • 14
  • 26

3 Answers3

2

Do you have ANY whitespace before the namespace? The namespace must occur before any output. This includes HTML to the browser. This also includes a UTF-8 Byte Order Mark (BOM) which can creep in from certain editors.

Try opening the file in an editor such as vi on Linux and it should show you if there's a BOM at the beginning.

Check How to fix "Headers already sent" error in PHP for information about other ways that output can start in a file.

Community
  • 1
  • 1
Colin M
  • 13,010
  • 3
  • 38
  • 58
  • I have checked, there is no BOM at the beginning. I also have no issue setting headers such as "Location: ", which require no white space before they are set. – SEoF Feb 13 '13 at 21:25
  • Are you sure you have no issue setting the header? Maybe your server is not showing the warnings, and only showing fatal errors (which namespace declarations are). If you have no output, no BOM, and you can truly execute a redirect header (the browser actually redirects)...then I'm lost. – Colin M Feb 13 '13 at 21:26
  • @SEoF Check your line endings as well, make sure they're not windows line endings. There is output coming from *somewhere* otherwise you wouldn't have this error. – Colin M Feb 13 '13 at 21:28
  • I'm certain. This is why I have asked the question, as I too am stumped. – SEoF Feb 13 '13 at 21:29
  • @SEoF So setting a `Location:` header causes the browser to redirect? Can you upload the raw file you're working with somewhere and provide a link to it? Not copied-and-pasted. – Colin M Feb 13 '13 at 21:30
  • Also, are you getting this error only when the file is included, or are you running this file directly (from the command line, for example) and still seeing this? – Colin M Feb 13 '13 at 21:31
  • I'm running this file directly. Unfortunately, I do not have any live hosting at the moment, and this would not help if the issue is, as suspected, a server configuration issue. – SEoF Feb 13 '13 at 21:32
  • 1
    The *only* server configuration issue I can think would even remotely relate is `auto_prepend_file`. – Colin M Feb 13 '13 at 21:34
  • auto_prepend_file is empty, and would also show up in the response when the the namespace declaration is commented out. Good suggestions though - a PHP setting I haven't heard of. – SEoF Feb 13 '13 at 21:38
  • @SEoF Not necessarily, it depends on what is being output and what the encoding of PHP/your console/etc are. But if you're sure that there's absolutely not a shot that anything is being output before the namespace declaration I can almost guarantee it isn't a server configuration issue and maybe you have discovered a bug. – Colin M Feb 13 '13 at 21:39
1

This happens when you have a file with a UTF-8 BOM as first character and you should remove all whitespaces before the start tag.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • I have checked, there is no BOM at the beginning. I also have no issue setting headers such as "Location: ", which require no white space before they are set. – SEoF Feb 13 '13 at 21:27
0

A re-install of the DSM (Synology OS) actually resolved the issue. Once done, some of the classes obtained from other frameworks had the "BOM" bytes at the beginning of the file, but not all. Simple solution to the BOM issues is to add a "BOM" remover into the auto-loader.

SEoF
  • 1,092
  • 14
  • 26