No code may exist outside of namespace {}

Description

When using a block notation for namespaces, there cannot be any other code outside the namespaced blocks. Here, the function foo() is outside the namespace block.

The error message is not strictly true: the declare command may be left out of the namespace, and so does any comments.

Example

<?php

namespace A {

}

function foo() {}

?>

Solutions

  • Move all the code inside a namespace block.

  • For global namespace, use the namespace {...} command without a name.

  • Switch to the namespace command without block: all the code will be in that namespace, until the next namespace command.