PHP Namespace not working - suspected PHP configuration issue
Asked Answered
N

3

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.

Nonalignment answered 13/2, 2013 at 21:19 Comment(2)
Does the same apply to third-party files using namespaces or only to ones you created?Deist
@Deist - All files using namespaces.Nonalignment
N
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.

Nonalignment answered 16/2, 2013 at 22:50 Comment(0)
C
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.

Comeuppance answered 13/2, 2013 at 21:22 Comment(10)
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.Nonalignment
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.Comeuppance
@Nonalignment 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.Comeuppance
I'm certain. This is why I have asked the question, as I too am stumped.Nonalignment
@Nonalignment 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.Comeuppance
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?Comeuppance
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.Nonalignment
The only server configuration issue I can think would even remotely relate is auto_prepend_file.Comeuppance
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.Nonalignment
@Nonalignment 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.Comeuppance
H
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.

Heterosporous answered 13/2, 2013 at 21:23 Comment(1)
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.Nonalignment
N
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.

Nonalignment answered 16/2, 2013 at 22:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.