命名空间
在线手册:中文  英文

定义命名空间

虽然任意合法的PHP代码都可以包含在命名空间中,但只有三种类型的代码受命名空间的影响,它们是:类,函数和常量。

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

Example #1 声明单个命名空间

<?php
namespace MyProject;

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

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

Example #2 声明单个命名空间

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

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


命名空间
在线手册:中文  英文

用户评论:

wyattbiker (2013-06-01 00:19:45)

<?php
//Even though you cant add anything before the 1st namespace, you can add something before subsequent namespaces.
namespace MyProject

function 
myfunc(){
    return 
1;
}
echo 
myfunc();
?>
<p>Here I can add stuff before the 2nd namespace</p>
<?php

namespace MyProject2

function 
myfunc(){
    return 
2;
}
echo 
myfunc();
?>
<p>Switch back to first namespace</p>
<?php
namespace MyProject;
echo 
myfunc();
?>

parsmizban.com (2012-06-12 11:02:39)

You can use this as a namespace declaration:

<?php
declare(encoding='UTF-8');
namespace 
parsmizban;

echo 
"parsmizban";
?>

huskyr at gmail dot com (2009-10-05 04:20:48)

"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 (2009-07-14 08:43:55)

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 (2009-04-14 12:02:21)

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!');
?>

David Drakard (2008-09-07 05:56:02)

I agree with SR, the new namespaces feature has solved a number of problems for me which would have required horrible coding to solve otherwise.
An example use:
Say you are making a small script, and write a class to connect to a database, calling it 'connection'. If you find your script useful and gradually expand it into a large application, you may want to rename the class. Without namespaces, you have to change the name and every reference to it (say in inheriting objects), possibly creating a load of bugs. With namespaces you can drop the related classes into a namespace with one line of code, and less chance of errors.
This is by no means one of the biggest problems namespaces solve; I would suggest reading about their advantages before citicising them. They provide an elegant solutions to several problems involved in creating complex systems.

Baptiste (2008-05-14 12:47:03)

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 (2008-04-01 11:11:34)

@ 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.

易百教程