定义命名空间

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

虽然任意合法的 PHP 代码都可以包含在命名空间中,但只有以下类型的代码受命名空间的影响,它们是:类(包括抽象类和 trait)、接口、函数和常量。

命名空间通过关键字 namespace 来声明。如果一个文件中包含命名空间,它必须在其它所有代码之前声明命名空间,除了一个以外:declare关键字。

Example #1 声明单个命名空间

<?php
namespace MyProject;

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

?>
在声明命名空间之前唯一合法的代码是用于定义源文件编码方式的 declare 语句。另外,所有非 PHP 代码包括空白符都不能出现在命名空间的声明之前:

Example #2 声明单个命名空间

<html>
<?php
namespace MyProject// 致命错误 - 命名空间必须是程序脚本的第一条语句
?>

另外,与 PHP 其它的语言特征不同,同一个命名空间可以定义在多个文件中,即允许将同一个命名空间的内容分割存放在不同的文件中。

User Contributed Notes

anisgazig at gmail dot com 21-Jan-2021 05:13
namespace statement  is defined at first of the php files. But
    before namespace declaration only three elements allowed.
      1.declare statement
      2.spaces
      3.comments
anisgazig at gmail dot com 14-Jan-2021 04:03
Namespace name are case-insensitive.
namespace App
and
namespace app
are same meaning.

Besides, Namespace keword are case-insensitive.
Namespace App
namespace App
and
NAMESPACE App
are same meaning.
kuzawinski dot marcin at NOSPAM dot gmail dot com 05-Aug-2014 09:52
If your code looks like this:

<?php
   
namespace NS;
?>

...and you still get "Namespace declaration statement has to be the very first statement in the script" Fatal error, then you probably use UTF-8 encoding (which is good) with Byte Order Mark, aka BOM (which is bad). Try to convert your files to "UTF-8 without BOM", and it should be ok.
FatBat 08-Aug-2013 09:46
Expanding on @danbettles note, it is better to always be explicit about which constant to use.

<?php
   
namespace NS;

   
define(__NAMESPACE__ .'\foo','111');
   
define('foo','222');

    echo
foo// 111.
   
echo \foo// 222.
   
echo \NS\foo  // 111.
   
echo NS\foo  // fatal error. assumes \NS\NS\foo.
?>
huskyr at gmail dot com 05-Oct-2009 04:20
"A file containing a namespace must declare the namespace at the top of the file before any other code"

It might be obvious, but this means that you *can* include comments and white spaces before the namespace keyword.

<?php
// Lots
// of
// interesting
// comments and white space

namespace Foo;
class
Bar {
}
?>
jeremeamia at gmail dot com 14-Jul-2009 08:43
You should not try to create namespaces that use PHP keywords. These will cause parse errors.

Examples:

<?php
namespace Project/Classes/Function; // Causes parse errors
namespace Project/Abstract/Factory; // Causes parse errors
?>
danbettles at yahoo dot co dot uk 14-Apr-2009 12:02
Regarding constants defined with define() inside namespaces...

define() will define constants exactly as specified.  So, if you want to define a constant in a namespace, you will need to specify the namespace in your call to define(), even if you're calling define() from within a namespace.  The following examples will make it clear.

The following code will define the constant "MESSAGE" in the global namespace (i.e. "\MESSAGE").

<?php
namespace test;
define('MESSAGE', 'Hello world!');
?>

The following code will define two constants in the "test" namespace.

<?php
namespace test;
define('test\HELLO', 'Hello world!');
define(__NAMESPACE__ . '\GOODBYE', 'Goodbye cruel world!');
?>
Baptiste 14-May-2008 12:47
There is nothing wrong with PHP namespaces, except that those 2 instructions give a false impression of package management.
... while they just correspond to the "with()" instruction of Javascript.

By contrast, a package is a namespace for its members, but it offers more (like deployment facilities), and a compiler knows exactly what classes are in a package, and where to find them.
Anonymous 01-Apr-2008 11:11
@ RS: Also, you can specify how your __autoload() function looks for the files. That way another users namespace classes cannot overwrite yours unless they replace your file specifically.
PHP8中文手册 站长在线 整理 版权归PHP文档组所有