-1

As said in the php reference

Namespaces are declared using the namespace keyword. A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the declare keyword.

But further we've the following code snippet in the reference:

<?php
namespace MyProject;

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }

namespace AnotherProject; //This namespace declaration doesn't located at the top of the file. It's unclear.

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }
?>

2 Answers2

1

One is declared at the top as first three lines under MyProject referes to MyProject namespace whereas other three under AnotherProject refers to AnotherProject namespace. If at least one namespace is declared as the top, file will be correctly parsed (namespace will be switched dinamically)

Just to be more clear, you can even do that

<?php
namespace MyProject {

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }
}

namespace AnotherProject {

const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */  }
}
?>

However is strongly not recommended to declare two namespaces inside same php script

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
  • It's obviously contradicts to `A file containing a namespace must declare the namespace at the top of the file before any other code`. Can you explain this? –  Mar 24 '14 at 10:46
  • @DmitryFucintv: why are you saying that? I can perfectly see namespace declaration at the top of the file, before all php code. As I told in my answer, at least one namespace should be declarated at the top, the others needs to be declared after (so not at the top) as all code under the declaration will refer to "new" namespace. Think about the scenario where you can declare three namespace at the top: at what namespace the code under should refers to? – DonCallisto Mar 24 '14 at 11:09
  • Thanks for your comment. But `at least one namespace should be declarated at the top` doesn't specified in the language reference. Why do you think so? –  Mar 24 '14 at 11:12
  • @DmitryFucintv empirical evidence. As deceze told into his answer, declare one namespace in the middle of a php file _without_ have one at the top result in a file that can't be parsed correctly – DonCallisto Mar 24 '14 at 11:14
  • I'm understand you. So I'm assume that it's spec's lack. –  Mar 24 '14 at 11:18
  • @DmitryFucintv: if you want to see it as a "lack" ok, it could be. But I suppose that this sentence is referred to files with single namespace so is perfectly legal – DonCallisto Mar 24 '14 at 11:20
1

You can switch to another namespace later on in the file, but if you are using namespaces at all, you have to declare a namespace as the first thing in the file. I.e., this does not work:

<?php

echo 'foo';

namespace Bar;

echo 'bar';
deceze
  • 510,633
  • 85
  • 743
  • 889
  • Thanks for your answer. But this namespaces deslared in the same file. AnotherProject namespace doesn't located at the top of this file. It's obviously contradicts to `A file containing a namespace must declare the namespace at the top of the file before any other code` –  Mar 24 '14 at 10:43
  • It does not. If the file uses a namespace at all, it must be declared at the top of the file. It does not say that no further namespace declaration may follow. – deceze Mar 24 '14 at 11:24
  • `If the file uses a namespace at all, it must be declared at the top of the file. It does not say that no further namespace declaration may follow.` It doesn't specified in the spec. –  Mar 24 '14 at 11:27
  • It is also not *not* specified in the documentation. What is written in the documentation is literally true and correct. One part says that you have to write namespace declarations at the top of the file if any are used, another part says that you can write further namespace declarations below. – deceze Mar 24 '14 at 11:35